async def on_message_edit(self, before, after): if not database.admin_cog(after.guild): return # prevent admin messages from showing in deleted messages admin_channel = 700538064224780298 if before.guild is None or before.channel.id == admin_channel: return # prevent bot messages from showing in edited messages if before.author.bot or before.content.startswith( 'http') or before.content == '': return spam = database.get_spam(after.guild) try: msg = f'{before.author.display_name} edited a message in {before.channel.mention}' embed = discord.Embed(description=msg) embed.set_thumbnail(url=before.author.avatar_url) embed.add_field(name='Before', value=before.content, inline=False) embed.add_field(name='After', value=after.content, inline=False) await spam.send(embed=embed) except discord.HTTPException: msg = f'{before.author.display_name} edited a message in {before.channel.mention}' embed = discord.Embed(description=msg) embed.set_thumbnail(url=before.author.avatar_url) embed.add_field(name='Before', value=before.content[:200] + '...', inline=False) embed.add_field(name='After', value=after.content[:200] + '...', inline=False) await spam.send(embed=embed)
async def assign_new_role(member: discord.Member): # send new role notifications to general chan = None channels = [c for c in member.guild.channels if 'general'] for c in channels: if 'general' in c.name: chan = c if chan is None: chan = database.get_spam(member.guild) if not database.in_members_table(member): database.add_member(member) pos, neg = database.get_rep(member) with open('files/ranks.json') as f: ranks = json.load(f)["host ranks"] for rank, reviews in ranks.items(): if pos >= reviews: if rank not in [role.name for role in member.guild.roles]: await member.guild.create_role(name=rank, hoist=True) role = discord.utils.get(member.guild.roles, name=rank) if role.name not in [r.name for r in member.roles]: await member.add_roles(role) await chan.send(embed=tools.single_embed( f':tada: {member.mention} has earned the **{role}** role!' ))
async def sub(self, ctx, method, member: discord.Member, points: int, *, message: str = None): if not await self.rep_cog_on(ctx): return if message is None: message = 'No additional information given.' if not database.in_members_table(ctx.author): database.add_member(ctx.author) bot_spam = database.get_spam(ctx.guild) message = f'**{ctx.author.display_name}** removed {points} {method} point(s) from **{member.display_name}**.\n'\ f'> {message}' if method == 'pos': database.sub_pos(member, points) await bot_spam.send(embed=tools.single_embed(message)) await self.assign_new_role(member) if method == 'neg': database.sub_neg(member, points) await bot_spam.send(embed=tools.single_embed_neg(message)) await ctx.send( embed=tools.single_embed(f'Your changes have been updated.'))
async def settings(self, ctx): if ctx.invoked_subcommand is None: prefix = database.get_prefix(ctx.guild)[0] spam = database.get_spam(ctx.guild) autorole = database.get_autorole(ctx.guild) administrative = database.get_administrative(ctx.guild) review_channel = database.get_review_channel(ctx.guild) # get cog status admin = database.admin_cog(ctx.guild) rep = database.rep_cog(ctx.guild) karma = database.karma_cog(ctx.guild) mw = database.mw_cog(ctx.guild) # assign on/off values cogs = [admin, rep, karma, mw] for c in range(len(cogs)): if cogs[c] is True: cogs[c] = 'On' else: cogs[c] = 'Off' embed = discord.Embed(title='Settings', color=discord.Color.blue()) embed.add_field(name='Prefix', value=f'`{prefix}`') embed.add_field(name='Autorole', value=autorole) msg = f'Spam: {spam.mention}\n'\ f'Admin: {administrative.mention}\n'\ f'Review (rep): {review_channel.mention}' embed.add_field(name='Channel Redirects', value=msg, inline=False) msg = f'Admin: `{cogs[0]}`\nRep: `{cogs[1]}`\nKarma: `{cogs[2]}`\n~~MW~~: `{cogs[3]}`' embed.add_field(name='Cogs', value=msg, inline=False) embed.set_thumbnail(url=ctx.guild.icon_url) await ctx.send(embed=embed)
async def warn(self, ctx, member: discord.Member, *, message: str = None): if not await self.admin_cog_on(ctx): return def check(react, user): return admin_channel == react.message.channel and not user.bot database.add_warning(member, message, ctx.author) warnings, messages = database.get_warnings(member) fmt = [f'{m[4]} - {m[3]} *Issuer: {discord.utils.get(ctx.guild.members, id=m[5]).display_name}' for m in messages] msg = f'You have received a warning from an administrator or mod in {ctx.guild.name}.\n> "{message}"' try: await member.send(embed=tools.single_embed_neg(msg)) except discord.Forbidden: spam = database.get_spam(ctx.guild) await spam.send(embed=f'{member.mention}: {msg}') embed = discord.Embed(color=discord.Color.red(), description=f'{member.mention} has been warned:\n"{message}"') embed.set_thumbnail(url=member.avatar_url) await ctx.send(embed=embed) if warnings >= 2: admin_channel = database.get_administrative(ctx.guild) msg = f'**{member.display_name}** has reached `{warnings}` warnings. Should they be kicked?\n' \ f'__Past Warnings__\n' + '\n'.join(fmt) embed = discord.Embed(color=discord.Color.red(), description=msg) embed.set_thumbnail(url=member.avatar_url) msg = await admin_channel.send(embed=embed) await msg.add_reaction('✔') await msg.add_reaction('❌') yes = 0 no = 0 limit = 4 while True: try: reaction, reactor = await self.client.wait_for('reaction_add', check=check, timeout=28800) if reaction.emoji == '✔': yes = reaction.count if yes >= limit: break if reaction.emoji == '❌': no = reaction.count if no >= limit: break except asyncio.TimeoutError: embed.add_field(name='Timeout', value='The vote has timed out.') await msg.edit(embed=embed) await msg.clear_reactions() return if yes > no: await member.kick(reason=f'You have been kicked from {ctx.guild.name}. The last warning was: "{message}"') embed.add_field(name='Vote completed', value=f'{member.display_name} has been kicked.') await msg.edit(embed=embed) await msg.clear_reactions() if no > yes: embed.add_field(name='Vote completed', value=f'{member.display_name} has been voted to stay.') await msg.edit(embed=embed) await msg.clear_reactions()
async def nick(self, ctx, *nickname): if len(nickname) < 1: msg = 'You name cannot be empty.' await ctx.send(embed=tools.single_embed(msg), delete_after=5) return spam = db.get_spam(ctx.guild) msg = f'{ctx.author.display_name} changed their nickname to {" ".join(nickname)}.' await ctx.author.edit(nick=' '.join([w.replace("'", "\'") for w in nickname])) await ctx.send(embed=tools.single_embed(msg)) await spam.send(embed=tools.single_embed(msg))
async def on_member_join(self, member): # add member to the database database.add_member(member) # if admin cog is not set, do not give autorole if not database.admin_cog(member.guild): return autorole = database.get_autorole(member.guild) if autorole is not None: try: await member.add_roles(autorole) spam = database.get_spam(member.guild) msg = f'{member.display_name} was given the role {autorole.name}' await spam.send(embed=tools.single_embed(msg)) print( f'* Giving {member.display_name} the autorole {autorole.name}' ) except Exception as e: spam = database.get_spam(member.guild) msg = f'Unable to give {member.display_name} the role {autorole.name}' await spam.send(embed=tools.single_embed(msg)) print( f'* Unable to give {member.display_name} the role {autorole.name}: {e}' ) # store nickname nicknames = [n[0] for n in database.get_member_nick_history(member)] if member.name not in nicknames: database.add_member_nick_history(member, member.name) # update the server statistics try: member_stats = self.client.get_channel(706099708434841621) members = [m for m in member.guild.members if not m.bot] await member_stats.edit(name=f'Members: {len(members)}') except Exception as e: print(f'Could not update server stats: {e}') # alert general on every 1000th member if len([m for m in member.guild.members if not m.bot]) % 1000 == 0: chan = self.client.get_channel(694013862667616310) msg = f'Welcome {member.mention} as our {len(member.guild.members)}th member!' await chan.send(embed=tools.single_embed(msg))
async def on_message_delete(self, message): # block certain messages from deleted message log admin_channel = 700538064224780298 if message is None or not database.admin_cog( message.guild ) or message.channel.id == admin_channel or message.author.bot: return msg = f'**A message was deleted**\n'\ f'Author: {message.author.display_name}\n'\ f'Channel: {message.channel.mention}\n'\ f'Message: {message.content}' embed = discord.Embed(color=discord.Color.red(), description=msg) embed.set_thumbnail(url=message.author.avatar_url) spam = database.get_spam(message.guild) await spam.send(embed=embed)