From 37d76e3c0381c998e685131e879d6f61ac097597 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 16 Jun 2020 19:18:35 +0100 Subject: [PATCH] Added reaction roles --- EnsoBot.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/EnsoBot.py b/EnsoBot.py index aea2ba75..71207f5f 100644 --- a/EnsoBot.py +++ b/EnsoBot.py @@ -26,7 +26,7 @@ if __name__ == '__main__': @client.event async def on_ready(): print('Bot is ready.') - await client.change_presence(activity=discord.Game(name="I'm scared")) + await client.change_presence(activity=discord.Game(name="Falling in Love 😍 😍")) # Bot ~Ping command in milliseconds @@ -48,6 +48,7 @@ async def on_command_error(ctx, error): await message.delete() +# Bot Event for handling missing argument error @client.event async def on_command_error(ctx, target: discord.member): if isinstance(target, commands.MissingRequiredArgument): @@ -59,6 +60,7 @@ async def on_command_error(ctx, target: discord.member): await message.delete() +# Bot Event for handling permission errors @client.event async def on_command_error(ctx, error): if isinstance(error, commands.CheckFailure): @@ -70,10 +72,13 @@ async def on_command_error(ctx, error): await message.delete() +# Bot event for new member joining, sending an embed introducing them to the server @client.event async def on_member_join(member): + # Set the channel id to "newpeople" channel = client.get_channel(669771571337887765) + # Surround with try/except to catch any exceptions that may occur try: embed = discord.Embed(title="\n**Welcome To Ensō!**", colour=discord.Colour(0x30e419)) @@ -84,8 +89,6 @@ async def on_member_join(member): "/image1.jpg?width=658&height=658") embed.set_image( url="https://cdn.discordapp.com/attachments/714671068941647933/717144047252275270/f4d7de6463d3ada02058a094fd6917ac.gif") - embed.set_footer(text=f"Hamothy#5619", - icon_url="https://media.discordapp.net/attachments/689525645734182916/718510466640642099/Rias_Gremory.png") embed.add_field( name="\u200b", value=f"Hello {member.mention}! We hope you enjoy your stay in this server! ", @@ -104,6 +107,87 @@ async def on_member_join(member): print(e) +# Allowing +@client.command(name="rolemenu") +async def role_menu(ctx): + try: + channel = client.get_channel(722347423913213992) + + embed = discord.Embed(title="**Role Menu: Ping-Able Roles**", colour=discord.Colour.orange()) + + embed.timestamp = datetime.datetime.utcnow() + + embed.set_thumbnail(url="https://media.discordapp.net/attachments/683490529862090814/715010931620446269" + "/image1.jpg?width=658&height=658") + embed.set_author(name="Hamothy", icon_url="https://cdn.discordapp.com/attachments/689525645734182916" + "/718510466640642099/Rias_Gremory.png") + embed.set_footer(text=f"{ctx.message.author}", + icon_url="https://media.discordapp.net/attachments/689525645734182916/718510466640642099/Rias_Gremory.png") + embed.add_field( + name="\u200b", + value="React to give yourself roles to be pinged for these events!", + inline=False) + embed.add_field( + name="\u200b", + value="<:MovieNight:722293598938333190> : `Movie Nights`", + inline=False) + embed.add_field( + name="\u200b", + value="<:Karaoke:722358251932483605> : `Karaoke Nights`", + inline=False) + embed.add_field( + name="\u200b", + value="<:EnsoBros:722360289345011743> : `Enso Bros Podcasts`", + inline=False) + embed.add_field( + name="\u200b", + value="<:GameNights:722502073769525268> : `Game Nights`", + inline=False) + + await channel.send(embed=embed) + except Exception as e: + print(e) + + +@client.event +async def on_raw_reaction_add(payload): + try: + if payload.message_id == 722514840559812649: + print(payload.emoji.name) + # Find a role corresponding to the Emoji name. + guild_id = payload.guild_id + guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds) + + role = discord.utils.find(lambda r: r.name == payload.emoji.name, guild.roles) + + if role is not None: + print(role.name + " was found!") + print(role.id) + member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members) + await member.add_roles(role) + print("done") + except Exception as e: + print(e) + + +@client.event +async def on_raw_reaction_remove(payload): + try: + if payload.message_id == 722514840559812649: + print(payload.emoji.name) + + guild_id = payload.guild_id + guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds) + role = discord.utils.find(lambda r: r.name == payload.emoji.name, guild.roles) + + if role is not None: + member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members) + await member.remove_roles(role) + except Exception as e: + print(e) + + +# Run the bot, allowing to come online try: client.run(API_TOKEN) except discord.errors.LoginFailure as e: