def setup_database(client: Bot): for guild in client.guilds: if not economy_collection.find_one({"_id": guild.id}): members = [] for member in guild.members: members.append({"id": member.id, "money": 0, "bank": 0}) economy_collection.insert_one({ "_id": guild.id, "name": guild.name, "members": members }) else: pass if not settings_collection.find_one({"_id": guild.id}): settings_collection.insert_one({ "_id": guild.id, "name": guild.name, "settings": {} }) else: pass
async def addbadpatterns(self, ctx: Context, *, patterns: str): patterns = patterns.lower().split(' ') settings = settings_collection.find_one({'_id': ctx.guild.id}) if 'bad_patterns' in settings.keys(): existing_patterns = settings['bad_patterns'] for pattern in existing_patterns: if pattern in patterns: pass else: existing_patterns.append(pattern) settings['bad_patterns'] = existing_patterns settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }}) else: settings['bad_patterns'] = patterns settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }})
async def suggest(self, ctx: Context, *, suggestion: str): ''' Suggest a feature to the staff of this server. ''' embed = discord.Embed(title="Server suggestion", description=f""" You have received a suggestion from `{ctx.author.name}#{ctx.author.discriminator}` ``` {suggestion} ``` """, color=discord.Color.green()) settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] if 'suggestion_channel' in settings.keys(): channel = discord.utils.get(ctx.guild.text_channels, id=settings['suggestion_channel']) if channel != None: await channel.send(embed=embed) await ctx.send( ":white_check_mark: Your request has been sent to the staff of this server!" ) else: await ctx.send( ":x: The suggestion channel doesn't exist or was deleted. Ask a moderator to set one with `-setsuggestionchannel <channel_id>`." ) else: await ctx.send( ":x: No suggestion channel is set. Ask a moderator to set one with `-setsuggestionchannel <channel_id>`" )
async def prefix(self, ctx: Context, prefix: str = ""): if prefix == "": settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] if 'prefix' in settings.keys(): embed = discord.Embed( title=f":shield: Prefix for **{ctx.guild.name}**", description=f"My prefix: `{settings['prefix']}`", color=discord.Color.blurple()) embed.set_footer( text= f"Requested by {ctx.author.name}#{ctx.author.discriminator}" ) await ctx.send(embed=embed) else: embed = discord.Embed( title=f":shield: Prefix for **{ctx.guild.name}**", description="My prefix: `-`", color=discord.Color.blurple()) embed.set_footer( text= f"Requested by {ctx.author.name}#{ctx.author.discriminator}" ) await ctx.send(embed=embed) else: settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] settings['prefix'] = prefix settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }})
async def on_member_remove(self, member: discord.Member): members_data = economy_collection.find_one({"_id": member.guild.id })['members'] i = 0 for user in members_data: if user['id'] == member.id: del members_data[i] break else: pass i += 1 economy_collection.find_one_and_update( {"_id": member.guild.id}, {"$set": { "members": members_data }}) settings = settings_collection.find_one({"_id": member.guild.id})['settings'] if 'welcome_channel' in settings.keys(): welcome_channel = get(member.guild.text_channels, id=settings['welcome_channel']) if 'leave_message' in settings.keys(): leave_message = settings['leave_message'] if "{ mention }" in leave_message: leave_message.replace("{ mention }", member.mention) embed = discord.Embed(title=f"**{member.guild.name}**", description=f"{leave_message}", color=discord.Color.dark_purple()) await welcome_channel.send(embed=embed) else: embed = discord.Embed( title=f"**{member.guild.name}**", description= f"Bye {member.mention}! We're sorry to see you go :(", color=discord.Color.dark_purple()) await welcome_channel.send(embed=embed) else: pass
def determine_prefix(bot: Bot, message: Message): if len(sys.argv) > 1 and sys.argv[1] == 'dev': return '}' else: if message.guild: settings = settings_collection.find_one({"_id": message.guild.id })['settings'] if 'prefix' in settings.keys(): return settings['prefix'] else: return '-' else: return '-'
async def setleavemessage(self, ctx: Context, *, message: str): ''' Set a message that the bot should send when a person leaves the server. Requires administrator permission. You can add `{ mention }` to the message and the bot will replace it with a mention of the user that left. ''' settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] settings['leave_message'] = message settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }}) await ctx.send(":white_check_mark: Successfully set leave message!")
async def setsuggestionchannel(self, ctx: Context, channel_id: int): ''' Set a channel in which to receive server suggestions. ''' settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] if discord.utils.get(ctx.guild.text_channels, id=channel_id): settings['suggestion_channel'] = channel_id settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }}) await ctx.send( ":white_check_mark: Successfully set suggestion channel!") else: await ctx.send(":x: That's not a valid channel ID!")
async def on_member_join(self, member: discord.Member): members_data = economy_collection.find_one({"_id": member.guild.id })['members'] members_data.append({"id": member.id, "money": 0, "bank": 0}) economy_collection.find_one_and_update( {"_id": member.guild.id}, {"$set": { "members": members_data }}) settings = settings_collection.find_one({"_id": member.guild.id})['settings'] if 'welcome_channel' in settings.keys(): welcome_channel = get(member.guild.text_channels, id=settings['welcome_channel']) if 'join_message' in settings.keys(): join_message = settings['join_message'] if "{ mention }" in join_message: join_message.replace("{ mention }", member.mention) embed = discord.Embed(title=f"**{member.guild.name}**", description=f"{join_message}", color=discord.Color.green()) await welcome_channel.send(embed=embed) else: embed = discord.Embed( title=f"**{member.guild.name}**", description= f"Welcome to {member.guild.name}! :partying_face:", color=discord.Color.green()) await welcome_channel.send(embed=embed) else: pass
async def setwelcomechannel(self, ctx: Context, channel_id: int): ''' Command to set a designated channel where the bot will send a message when a user joins/leaves the server. Requires you to have administrator permission. ''' settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] if discord.utils.get(ctx.guild.text_channels, id=channel_id): settings['welcome_channel'] = channel_id settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }}) await ctx.send( ":white_check_mark: Successfully set welcome channel!") else: await ctx.send(":x: That's not a valid channel ID!")
async def on_message(self, message: discord.Message): settings = settings_collection.find_one( {"_id": message.guild.id} )['settings'] if 'automod' in settings.keys(): if settings['automod'] == 'on': if 'bad_patterns' in settings.keys(): patterns = settings['bad_patterns'] for word in message.content.split(' '): if word.lower() in patterns: await message.delete() else: pass else: pass
async def setautomod(self, ctx: Context, mode: str): settings = settings_collection.find_one({"_id": ctx.guild.id})['settings'] if mode == 'on': settings['automod'] = mode elif mode == 'off': settings['automod'] = mode else: await ctx.send( f":x: Invalid option `{mode}`. Please pick between `on` and `off`" ) return await ctx.send( f':white_check_mark: Successfully set automod to `{mode}`') settings_collection.find_one_and_update( {"_id": ctx.guild.id}, {"$set": { "settings": settings }})