async def on_message(self, message): """Rolechannel message handler.""" if message.guild is None: return if db.get_setting(message.guild.id, "rolepicker_enabled") == 0: return if message.channel.id == db.get_setting(message.guild.id, "rolepicker_channel"): if not message.author == self.bot.user: command = message.content[0] rolename = message.content[1:].strip() if command in ["+", "-"]: case = db.get_setting(message.guild.id, "rolepicker_case") role = message.guild.get_role(db.rolepicker_role(message.guild.id, rolename, caps=(case != 0))) if role is None: await message.channel.send(":warning: Role not found") else: if command == "+": await message.author.add_roles(role) await message.channel.send(f"Added you the role **{role.name}!**") elif command == "-": await message.author.remove_roles(role) await message.channel.send(f"Removed the role **{role.name}** from you") else: await message.channel.send( f":warning: Invalid role command `{command}`\nUse `+` to add a role and `-` to remove a role") await asyncio.sleep(5) try: await message.delete() except discord.errors.NotFound: pass
async def on_message_delete(self, message): """Listener that gets called when any message is deleted.""" # ignore bots if message.author.bot: return # ignore DMs if message.guild is None: return # ignore empty messages if len(message.content) == 0 and len(message.attachments) == 0: return # ignored channels if (db.query( "select * from deleted_messages_mask where channel_id = ?", (message.channel.id, ), ) is not None): return channel_id = db.get_setting(message.guild.id, "deleted_messages_channel") if channel_id is None: return channel = message.guild.get_channel(channel_id) if channel is None or message.channel == channel: return try: await channel.send(embed=util.message_embed(message)) except discord.errors.Forbidden: pass
async def on_member_remove(self, member): """Called when member leaves a guild.""" channel_id = db.get_setting(member.guild.id, "goodbye_channel") if channel_id is None: return channel = member.guild.get_channel(channel_id) if channel is None: return logger.warning(f"Cannot say goodbye to {member} from {member.guild.name} (invalid channel)") message_format = db.get_setting(member.guild.id, "goodbye_message") if message_format is None: message_format = "Goodbye {mention} ( **{user}** )" await channel.send(util.create_goodbye_message(member, member.guild, message_format)) logger.info(f"Said goodbye to {member.name} from {member.guild.name}")
async def on_message(self, message): """Rolechannel message handler.""" # make sure bot cache is ready if not self.bot.is_ready: return if message.guild is None: return if db.get_setting(message.guild.id, "rolepicker_enabled") == 0: return if message.channel.id == db.get_setting(message.guild.id, "rolepicker_channel"): if not message.author.bot: command = message.content[0] rolename = message.content[1:].strip() if command in ["+", "-"]: role = message.guild.get_role( db.rolepicker_role(message.guild.id, rolename)) try: if role is None: await message.channel.send( f":warning: Role `{rolename}` not found") else: if command == "+": await message.author.add_roles(role) await message.channel.send(embed=discord.Embed( description= f"Added {role.mention} to your roles")) elif command == "-": await message.author.remove_roles(role) await message.channel.send(embed=discord.Embed( description= f"Removed {role.mention} from your roles")) except discord.errors.Forbidden: pass else: await message.channel.send( f":warning: Unknown action `{command}`\nUse `+` to add roles or `-` to remove them." ) await asyncio.sleep(5) try: await message.delete() except discord.errors.NotFound: pass
async def everyone_or_manager(ctx): if ctx.author.guild_permissions.manage_guild: return True setting = db.get_setting(ctx.guild.id, "custom_commands_everyone") if setting != 0: return True else: raise commands.MissingPermissions(["manage_server"])
async def on_member_ban(self, guild, user): """Called when user gets banned from a server.""" channel_id = db.get_setting(guild.id, "bans_channel") if channel_id is None: return channel = guild.get_channel(channel_id) if channel is None: return logger.warning(f"Cannot announce ban of {user} from {guild.name} (invalid channel)") await channel.send(f"**{user.name}** (`{user.id}`) has been permanently banned") logger.info(f"{user} was just banned from {guild.name}")
async def on_member_join(self, member): """Called when a new member joins a guild.""" channel_id = db.get_setting(member.guild.id, "welcome_channel") if channel_id is not None: channel = member.guild.get_channel(channel_id) if channel is None: logger.warning( f"Cannot welcome {member} to {member.guild.name} (invalid channel)" ) else: message_format = db.get_setting(member.guild.id, "welcome_message") if message_format is None: message_format = "Welcome **{username}** {mention} to **{server}**" try: if db.get_setting(member.guild.id, "welcome_embed") == 0: await channel.send( util.create_welcome_without_embed( member, member.guild, message_format)) else: await channel.send(embed=util.create_welcome_embed( member, member.guild, message_format)) except discord.errors.Forbidden: pass # add autorole role = member.guild.get_role( db.get_setting(member.guild.id, "autorole")) if role is not None: try: await member.add_roles(role) except discord.errors.Forbidden: logger.error( f"Trying to add autorole failed in {member.guild.name} (no permissions)" )
async def on_member_ban(self, guild, user): """Called when user gets banned from a server.""" channel_id = db.get_setting(guild.id, "bans_channel") if channel_id is None: return channel = guild.get_channel(channel_id) if channel is None: return logger.warning( f"Cannot announce ban of {user} from {guild.name} (invalid channel)" ) try: await channel.send( f":hammer: **{user}** (`{user.id}`) has just been banned") except discord.errors.Forbidden: pass
async def on_message_delete(self, message): """Listener that gets called when any message is deleted.""" # ignore DMs if message.guild is None: return channel_id = db.get_setting(message.guild.id, "deleted_messages_channel") if channel_id is None: return channel = message.guild.get_channel(channel_id) if channel is None or message.channel == channel: return try: await channel.send(embed=util.message_embed(message)) except discord.errors.Forbidden: pass
async def welcome_message(self, ctx, *, message): """Change the welcome message. Usage: >logger welcome message <message...> >logger welcome message default """ if message.lower() == "default": db.update_setting(ctx.guild.id, "welcome_message", None) return await ctx.send("Welcome message has been reset to default.") db.update_setting(ctx.guild.id, "welcome_message", message) if db.get_setting(ctx.guild.id, "welcome_embed") == 0: preview = util.create_welcome_without_embed( ctx.author, ctx.guild, message) await ctx.send( f"New welcome message set:\n```{message}```\n> Preview:") await ctx.send(preview) else: preview = util.create_welcome_embed(ctx.author, ctx.guild, message) await ctx.send( f"New welcome message set:\n```{message}```Preview:", embed=preview)
async def on_message(self, message): """Listener that gets called on every message.""" # make sure cache is ready if not self.bot.is_ready: return # ignore DMs if message.guild is None: return # votechannels data = db.query( """SELECT channeltype FROM votechannels WHERE guild_id = ? and channel_id = ?""", (message.guild.id, message.channel.id), ) if data is not None: if data[0][0] == "rating": for e in ["0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣"]: await message.add_reaction(e) else: await message.add_reaction(emojis.UPVOTE) await message.add_reaction(emojis.DOWNVOTE) # xp gain message_xp = util.xp_from_message(message) currenthour = message.created_at.hour db.add_activity(message.guild.id, message.author.id, message_xp, currenthour) # if bot account, ignore everything after this if message.author.bot: return if db.get_setting(message.guild.id, "autoresponses") == 1: await self.easter_eggs(message) # log emojis unicode_emojis = util.find_unicode_emojis(message.content) custom_emojis = util.find_custom_emojis(message.content) if unicode_emojis or custom_emojis: db.log_emoji_usage(message, custom_emojis, unicode_emojis) # level up message announce = db.get_setting(message.guild.id, "levelup_toggle") if announce != 0: activity_data = db.get_user_activity(message.guild.id, message.author.id) if activity_data is None: return xp = sum(activity_data) level_before = util.get_level(xp - message_xp) level_now = util.get_level(xp) if level_now > level_before: try: await message.channel.send( f"{message.author.mention} just leveled up! (level **{level_now}**)", delete_after=5, ) except discord.errors.Forbidden: pass
async def on_message(self, message): """Listener that gets called on every message.""" # ignore DMs if message.guild is None: return # votechannels if db.query("select * from votechannels where guild_id = ? and channel_id = ?", (message.guild.id, message.channel.id)) is not None: await message.add_reaction(self.bot.get_emoji( db.query("select id from emojis where name = 'upvote'")[0][0])) await message.add_reaction(self.bot.get_emoji( db.query("select id from emojis where name = 'downvote'")[0][0])) # xp gain message_xp = util.xp_from_message(message) currenthour = message.created_at.hour db.add_activity(message.guild.id, message.author.id, message_xp, currenthour) # if bot account, ignore everything after this if message.author.bot: return # stfu if self.stfu_regex.findall(message.content) and random.randint(0, 1) == 0: try: await message.channel.send("no u") except discord.errors.Forbidden: pass # hi if message.content.lower().strip("!.?~ ") == "hi" and random.randint(0, 19) == 0: try: await message.channel.send('hi') except discord.errors.Forbidden: pass # git gud if message.content.lower().startswith("git "): gitcommand = re.search(r'git (\S+)', message.content) if gitcommand is not None: try: gitcommand = gitcommand.group(1) if gitcommand == "--help": msg = "```\n" \ "usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]\n" \ " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n" \ " [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\n" \ " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n" \ " <command> [<args>]```" await message.channel.send(msg) elif gitcommand == "--version": await message.channel.send("`git version 2.17.1`") elif gitcommand in ["commit", "push", "pull", "checkout", "status", "init", "add"]: pass else: await message.channel.send(f"`git: '{gitcommand}' is not a git command. See 'git --help'.`") except discord.errors.Forbidden: pass # log emojis unicode_emojis = util.find_unicode_emojis(message.content) custom_emojis = util.find_custom_emojis(message.content) if unicode_emojis or custom_emojis: db.log_emoji_usage(message, custom_emojis, unicode_emojis) # level up message announce = util.int_to_bool(db.get_setting(message.guild.id, "levelup_toggle")) if announce: activity_data = db.get_user_activity(message.guild.id, message.author.id) if activity_data is None: return xp = sum(activity_data) level_before = util.get_level(xp-message_xp) level_now = util.get_level(xp) if level_now > level_before: try: await message.channel.send(f"{message.author.mention} just leveled up! (level **{level_now}**)") except discord.errors.Forbidden: pass