async def on_message(self, ctx): # listen to every messages t hat us ers type get_guild_delete_timer() if ctx.author == self.client.user: return timers = get_json_data('data', 'timer.json') delete_time = timers.get(str(ctx.guild.id), 30) print(get_guild_delete_timer()) collection = get_database('triggers')[1] id_filter = {'_id': ctx.guild.id} cursor = get_database_data(collection, id_filter) trigger_response = {} if cursor: trigger_response = dict(cursor) msg = ctx.content.strip() if not msg.startswith(self.client.command_prefix): if msg in trigger_response.keys(): embed = get_embed(msg, trigger_response[msg]) await ctx.channel.send(embed=embed) else: trigger = botfile.get_clean_trigger_from(msg, trigger_response) if msg in trigger_response.keys( ) or botfile.is_user_response_valid(msg, trigger_response): embed = get_embed(name=trigger, value=trigger_response[trigger]) await ctx.channel.send( embed=embed, delete_after=get_guild_delete_timer()) elif ctx.content == 'raise-exception': raise discord.DiscordException
async def update_command(self, ctx): embed_color = discord.Colour.blue() text = f'Enter a **trigger** name : (Or type **c** To **Cancel**)' embed = get_embed(name='Update', value=text, color=embed_color) cancel_response = 'Command **cancelled!**' current_user = ctx.author file_name = get_server_data_file_name(ctx.guild.name, ctx.guild.id) path = get_absolute_file_path('data', file_name) collection = get_database('triggers')[1] id_filter = {'_id': ctx.guild.id} cursor = get_database_data(collection, id_filter) trigger_response = {} #if database exists if cursor: trigger_response = dict(cursor) self.client.unload_extension(BASIC_COG) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) trigger = await self.client.wait_for( 'message', check=lambda m: m.author == current_user) message = trigger trigger = trigger.content.strip() if trigger.lower() == 'c': self.client.load_extension(BASIC_COG) embed = get_embed(name='Cancelled!', value=cancel_response, color=embed_color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) return elif trigger in trigger_response.keys(): text = f'''The actual **response** of your **trigger** is "**{trigger_response[trigger]}**" Enter a new **response** to update the **trigger**: (Or type **c** To **Cancel**)''' embed = get_embed(name="Updating Trigger", value=text, color=embed_color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) response = await self.client.wait_for( 'message', check=lambda m: m.author == current_user) message = response if response.content.lower().strip() == 'c': embed = get_embed(name='Cancelled!', value=cancel_response, color=embed_color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG) return response = response.content.strip() trigger_response[trigger] = response update_database_data(filter_id=id_filter, value=response, key=trigger) update_file_in_github_repo( f'data/{ctx.guild.name}-{ctx.guild.id}.json', trigger_response, ) update_local_server_file(trigger_response, path) self.client.load_extension(BASIC_COG) text = f'Updated **trigger**: "**{trigger}**" with new **response**: "**{trigger_response[trigger]}**"' embed = get_embed(name='Updated!', value=text, color=embed_color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) else: embed = get_embed( name='Error!', value=f'**trigger** name "{trigger}" does not exist', color=discord.Colour.red()) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG)
async def delete_command(self, ctx): self.client.unload_extension(BASIC_COG) # delete an entry (key) trigger and (value) response from the dictionary cancel_response = 'command **cancelled!**' file_name = get_server_data_file_name(ctx.guild.name, ctx.guild.id) path = get_absolute_file_path('data', file_name) collection = get_database('triggers')[1] id_filter = {'_id': ctx.guild.id} cursor = get_database_data(collection, id_filter) trigger_response = {} if cursor: trigger_response = dict(cursor) current_user = ctx.author text = f'Enter the **trigger\'s** name to **delete** it\'s entry (Or type **c** to **Cancel**):' color = discord.Colour.red() embed = get_embed(name="Delete", value=text, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) trigger = await self.client.wait_for( 'message', check=lambda m: m.author == current_user) message = trigger trigger = trigger.content.strip() if (trigger.lower() == 'c'): embed = get_embed(name='Cancelled!', value=cancel_response, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG) return else: if trigger in trigger_response.keys(): response = trigger_response[trigger] post = {'_id': int(ctx.guild.id)} #delete one entry by id from database collection.update_one(post, {'$unset': {trigger: response}}) del trigger_response[trigger] msg = 'delete' update_file_in_github_repo( f'data/{ctx.guild.name}-{ctx.guild.id}.json', trigger_response, msg) update_local_server_file(trigger_response, path) text = f'**Trigger**: "{trigger}"\n**Response**: "{response}" was **deleted** with success' embed = get_embed(name="Deleted!", value=text, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) else: embed = get_embed(name="Error!", value=f'**{trigger}** does not exist!', color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG)
async def add_command(self, ctx): cancel_response = 'command **cancelled!**' # admin to add a trigger, response to the (key) trigger and (value) response dictionary current_user = ctx.author file_name = get_server_data_file_name(ctx.guild.name, ctx.guild.id) path = get_absolute_file_path('data', file_name) collection = get_database('triggers')[1] id_filter = {'_id': ctx.guild.id} cursor = get_database_data(collection, id_filter) trigger_response = {} if cursor: trigger_response = dict(cursor) color = discord.Colour.green() text = f'**Add** a new **trigger**: (Or type **c** To **Cancel**)' embed = get_embed(name="Add", value=text, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) self.client.unload_extension(BASIC_COG) trigger = await self.client.wait_for( 'message', check=lambda m: m.author == current_user) message = trigger trigger = trigger.content.strip() if (trigger.lower() == 'c'): embed = get_embed(name='Cancelled!', value=cancel_response, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG) return else: if trigger in trigger_response.keys(): embed = get_embed( name='Error!', value=f'*Trigger**: "{trigger}" already exists!', color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG) return else: text = f'**Add** a **response** to the **trigger**: (Or type **c** to **Cancel**):' embed = get_embed(name="Adding a Response", value=text, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) response = await self.client.wait_for( 'message', check=lambda m: m.author == current_user) message = response response = response.content.strip() if (response.lower() == 'c'): embed = get_embed(name='Cancelled!', value=cancel_response, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) self.client.load_extension(BASIC_COG) return else: text = f'Trigger: "{trigger}"\nResponse: "{response}" added with success!!' embed = get_embed(name="Added!", value=text, color=color) await ctx.send(embed=embed, delete_after=get_guild_delete_timer()) await ctx.message.delete(delay=get_guild_delete_timer()) await message.delete(delay=get_guild_delete_timer()) trigger_response[trigger] = response post = {'_id': int(ctx.guild.id)} update_file_in_github_repo( f'data/{ctx.guild.name}-{ctx.guild.id}.json', trigger_response) update_local_server_file(trigger_response, path) collection.update_one(post, {'$set': {trigger: response}}) self.client.load_extension(BASIC_COG)
print("Bot has been removed") @client.event async def on_member_join(member): pass # greet people when they join the server # await member.create_dm() # await member.dm_channel.send( # f'Hi {member.name}, welcome to my Discord server!') def load_cogs(path, folder): cogs = [i[:-3] for i in os.listdir(path) if i.endswith('.py')] for cog in cogs: client.load_extension(f'{folder}.{cog}') def get_collection(): return COLLECTION CLIENT, COLLECTION = mongodb.get_database('triggers') logging.basicConfig(filename='err.log', filemode='w', level=logging.INFO) load_cogs(os.path.join(os.getcwd(), 'cogs'), 'cogs') client.run(TOKEN) CLIENT.close()