2019-08-10 13:09:33 +02:00
|
|
|
import shelve
|
2019-08-10 17:42:14 +02:00
|
|
|
from discord import NotFound, InvalidArgument, Embed
|
2019-08-10 13:09:33 +02:00
|
|
|
from discord.ext import commands
|
2019-08-10 11:19:49 +02:00
|
|
|
|
2019-08-10 17:42:14 +02:00
|
|
|
bot = commands.Bot(command_prefix="%") # Set bot object and command prefix
|
|
|
|
bot.remove_command("help") # Override default help command
|
2019-08-10 11:19:49 +02:00
|
|
|
|
2019-08-10 13:09:33 +02:00
|
|
|
|
|
|
|
def set_config(guild, conf):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""guild : guild object, conf: shelve object
|
|
|
|
Set configuration for server on configuration file"""
|
2019-08-10 13:09:33 +02:00
|
|
|
print(f"Set config for {guild.name}")
|
2019-08-10 17:42:14 +02:00
|
|
|
conf[str(guild.id)] = dict() # Create default dictionary
|
|
|
|
for opt in [["default_role_id", ""], ["reaction_messages", dict()]]: # Set up each option in configuration file
|
2019-08-10 16:07:53 +02:00
|
|
|
conf[str(guild.id)][opt[0]] = opt[1]
|
2019-08-10 13:09:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_ready():
|
2019-08-10 17:42:14 +02:00
|
|
|
"""When bot start check if guilds doesn't need configuration"""
|
2019-08-10 13:09:33 +02:00
|
|
|
with shelve.open("config.conf", writeback=True) as conf:
|
2019-08-10 17:42:14 +02:00
|
|
|
for g in bot.guilds: # Check each guild
|
2019-08-10 13:09:33 +02:00
|
|
|
if str(g.id) not in conf:
|
|
|
|
set_config(g, conf)
|
|
|
|
|
|
|
|
print("FurrMula Bot online !")
|
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_guild_join(guild):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""guild: guild object
|
|
|
|
If the bot get a new guild, setup the configuration"""
|
2019-08-10 13:09:33 +02:00
|
|
|
with shelve.open("config.conf", writeback=True) as conf:
|
|
|
|
set_config(guild, conf)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_member_join(member):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""member: member object
|
|
|
|
If available, add a default role to new members of guild"""
|
2019-08-10 13:09:33 +02:00
|
|
|
with shelve.open("config.conf") as conf:
|
|
|
|
if "default_role_id" in conf[str(member.guild.id)] and conf[str(member.guild.id)]["default_role_id"]:
|
2019-08-10 18:03:56 +02:00
|
|
|
role = member.guild.get_role(conf[str(member.guild.id)]["default_role_id"][0]) # Get the role from guild
|
2019-08-10 13:09:33 +02:00
|
|
|
await member.add_roles(role)
|
|
|
|
|
|
|
|
|
2019-08-10 16:07:53 +02:00
|
|
|
@bot.event
|
|
|
|
async def on_raw_reaction_add(payload):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""payload: RawReaction payload object
|
|
|
|
When a reaction is add check if is a role reaction and apply the request"""
|
|
|
|
guild = bot.get_guild(payload.guild_id) # Get the current guild
|
|
|
|
member = guild.get_member(payload.user_id) # Get the current member
|
2019-08-10 16:07:53 +02:00
|
|
|
await reaction_role(payload.message_id, guild, str(payload.emoji), member, True)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.event
|
|
|
|
async def on_raw_reaction_remove(payload):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""payload: RawReaction payload object
|
|
|
|
When a reaction is remove check if is a role reaction and apply the request"""
|
|
|
|
guild = bot.get_guild(payload.guild_id) # Get the current guild
|
|
|
|
member = guild.get_member(payload.user_id) # Get the current member
|
2019-08-10 16:07:53 +02:00
|
|
|
await reaction_role(payload.message_id, guild, str(payload.emoji), member, False)
|
|
|
|
|
|
|
|
|
|
|
|
async def reaction_role(message_id, guild, emoji, member, state):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""message_id: (int) the id of the message, guild: guild object, emoji: the reaction emoji, member: member object,
|
|
|
|
state: (bool) add or remove target role
|
|
|
|
Check the configuration and if available, set the link role to the player"""
|
2019-08-10 16:07:53 +02:00
|
|
|
with shelve.open("config.conf") as conf:
|
2019-08-10 17:42:14 +02:00
|
|
|
# Avoid bot user and check if message and reaction are in configuration
|
2019-08-10 16:07:53 +02:00
|
|
|
if not member.bot and \
|
|
|
|
"reaction_messages" in conf[str(guild.id)] and \
|
|
|
|
message_id in conf[str(guild.id)]["reaction_messages"] and \
|
|
|
|
emoji in conf[str(guild.id)]["reaction_messages"][message_id]:
|
|
|
|
|
2019-08-10 18:03:56 +02:00
|
|
|
# If member has default new role, give him default role
|
|
|
|
def_new_role = guild.get_role(conf[str(guild.id)]["default_role_id"][0])
|
|
|
|
if def_new_role in member.roles:
|
|
|
|
def_role = guild.get_role(conf[str(guild.id)]["default_role_id"][1])
|
|
|
|
await member.remove_roles(def_new_role)
|
|
|
|
await member.add_roles(def_role)
|
|
|
|
|
2019-08-10 17:42:14 +02:00
|
|
|
role = guild.get_role(conf[str(guild.id)]["reaction_messages"][message_id][emoji]) # Get the target role
|
2019-08-10 16:07:53 +02:00
|
|
|
|
2019-08-10 17:42:14 +02:00
|
|
|
# State-dependent action
|
2019-08-10 16:07:53 +02:00
|
|
|
if state:
|
|
|
|
await member.add_roles(role)
|
|
|
|
else:
|
|
|
|
await member.remove_roles(role)
|
|
|
|
|
|
|
|
|
2019-08-10 17:42:14 +02:00
|
|
|
@bot.command(name="help")
|
|
|
|
@commands.has_permissions(administrator=True)
|
|
|
|
async def help_cmd(ctx):
|
|
|
|
embed = Embed(title="Help", description="", color=0xffff00)
|
2019-08-10 18:03:56 +02:00
|
|
|
embed.add_field(name="Set default role", value="``set_default_roles @new_default @default`` to set new member "
|
|
|
|
"default role and default role, if any role mentioned this disable "
|
|
|
|
"the option")
|
2019-08-10 17:42:14 +02:00
|
|
|
embed.add_field(name="Default role", value="``default_role`` show the new member default role")
|
|
|
|
embed.add_field(name="Reaction message", value="""``reaction_message <action> <message id> <...>``
|
|
|
|
**__actions :__**
|
|
|
|
``set <message id>`` to set a reaction message
|
|
|
|
``unset <message id>`` to unset a reaction message
|
|
|
|
``add <message id> <reaction emoji> @role`` to add a reaction on set message
|
|
|
|
``remove <message id> <reaction emoji>`` to remove a reaction on set message""")
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
|
|
|
@help_cmd.error
|
|
|
|
async def help_cmd_error(ctx, error):
|
|
|
|
"""ctx: context object, error: raised error
|
|
|
|
Manage help_cmd command errors"""
|
|
|
|
if isinstance(error, commands.errors.MissingPermissions):
|
|
|
|
await ctx.send("You are missing Administrator permission to run this command ! :no_entry:")
|
2019-08-10 13:09:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bot.command()
|
2019-08-10 16:07:53 +02:00
|
|
|
@commands.guild_only()
|
2019-08-10 17:42:14 +02:00
|
|
|
@commands.has_permissions(administrator=True)
|
2019-08-10 18:03:56 +02:00
|
|
|
async def set_default_roles(ctx):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""ctx: context object
|
|
|
|
Set the default role of a guild"""
|
2019-08-10 13:09:33 +02:00
|
|
|
with shelve.open("config.conf", writeback=True) as conf:
|
2019-08-10 18:03:56 +02:00
|
|
|
if len(ctx.message.role_mentions) == 2: # Accept only two mentioned role
|
|
|
|
conf[str(ctx.guild.id)]["default_role_id"] = [ctx.message.role_mentions[0].id,
|
|
|
|
ctx.message.role_mentions[1].id] # Set in configuration
|
|
|
|
await ctx.send(f"Roles ``{ctx.message.role_mentions[0].name}`` and ``{ctx.message.role_mentions[1].name}``"
|
|
|
|
f" set to default role :white_check_mark:")
|
2019-08-10 17:42:14 +02:00
|
|
|
elif len(ctx.message.role_mentions) == 0: # If any role, disable default role option
|
2019-08-10 18:03:56 +02:00
|
|
|
conf[str(ctx.guild.id)]["default_role_id"] = "" # Set in configuration
|
2019-08-10 17:42:14 +02:00
|
|
|
await ctx.send("Default role disabled :warning:")
|
|
|
|
else: # If invalid arguments
|
|
|
|
raise commands.BadArgument
|
|
|
|
|
|
|
|
|
2019-08-10 18:03:56 +02:00
|
|
|
@set_default_roles.error
|
|
|
|
async def set_default_roles_error(ctx, error):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""ctx: context object, error: raised error
|
2019-08-10 18:03:56 +02:00
|
|
|
Manage set_default_roles command errors"""
|
2019-08-10 17:42:14 +02:00
|
|
|
if isinstance(error, commands.BadArgument):
|
|
|
|
await ctx.send("Invalid mentioned role ! :x:")
|
|
|
|
if isinstance(error, commands.errors.MissingPermissions):
|
|
|
|
await ctx.send("You are missing Administrator permission to run this command ! :no_entry:")
|
2019-08-10 13:09:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bot.command()
|
2019-08-10 16:07:53 +02:00
|
|
|
@commands.guild_only()
|
2019-08-10 17:42:14 +02:00
|
|
|
@commands.has_permissions(administrator=True)
|
2019-08-10 13:09:33 +02:00
|
|
|
async def default_role(ctx):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""ctx: context object
|
2019-08-10 18:10:38 +02:00
|
|
|
Show guild default roles"""
|
2019-08-10 13:09:33 +02:00
|
|
|
with shelve.open("config.conf") as conf:
|
2019-08-10 18:10:38 +02:00
|
|
|
new_role = ctx.guild.get_role(conf[str(ctx.guild.id)]["default_role_id"][0]).name # Get new role
|
|
|
|
role = ctx.guild.get_role(conf[str(ctx.guild.id)]["default_role_id"][1]).name # Get default role
|
|
|
|
await ctx.send(f"Guild default roles: ``{new_role}`` and ``{role}``")
|
2019-08-10 17:42:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
@default_role.error
|
|
|
|
async def default_role_error(ctx, error):
|
|
|
|
"""ctx: context object, error: raised error
|
|
|
|
Manage default_role command errors"""
|
|
|
|
if isinstance(error, commands.errors.MissingPermissions):
|
|
|
|
await ctx.send("You are missing Administrator permission to run this command ! :no_entry:")
|
2019-08-10 11:19:49 +02:00
|
|
|
|
|
|
|
|
2019-08-10 16:07:53 +02:00
|
|
|
@bot.command()
|
|
|
|
@commands.guild_only()
|
2019-08-10 17:42:14 +02:00
|
|
|
@commands.has_permissions(administrator=True)
|
|
|
|
async def reaction_message(ctx, action=None, message_id=None, reaction=None):
|
|
|
|
"""ctx: context object, action: (str) command action, message_id: (str) the message id,
|
|
|
|
reaction: (str) reaction emoji
|
|
|
|
Set or unset a reaction message and add reaction emoji link with roles"""
|
|
|
|
if action not in ["set", "unset", "add", "remove"]: # Check if action is correct
|
|
|
|
raise commands.BadArgument("argument")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
2019-08-10 17:42:14 +02:00
|
|
|
try: # Check message id
|
2019-08-10 16:07:53 +02:00
|
|
|
message = await ctx.fetch_message(int(message_id))
|
|
|
|
except (TypeError, ValueError, NotFound):
|
2019-08-10 17:42:14 +02:00
|
|
|
raise commands.BadArgument("message id")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
|
|
|
with shelve.open("config.conf", writeback=True) as conf:
|
2019-08-10 17:42:14 +02:00
|
|
|
if action == "set": # Set a reaction message
|
|
|
|
if message.id in conf[str(ctx.guild.id)]["reaction_messages"]: # Check if not already set
|
|
|
|
raise commands.BadArgument("already set")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
2019-08-10 17:42:14 +02:00
|
|
|
conf[str(ctx.guild.id)]["reaction_messages"][message.id] = dict() # Add to configuration
|
|
|
|
await ctx.send("Message set :white_check_mark:")
|
|
|
|
elif action == "unset": # Unset a reaction message
|
|
|
|
try: # Check if the message is set when remove
|
2019-08-10 16:07:53 +02:00
|
|
|
del conf[str(ctx.guild.id)]["reaction_messages"][message.id]
|
|
|
|
except KeyError:
|
2019-08-10 17:42:14 +02:00
|
|
|
raise commands.BadArgument("not set")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
2019-08-10 17:42:14 +02:00
|
|
|
await ctx.send("Message remove :wastebasket:")
|
|
|
|
else: # Add and Remove reaction actions
|
|
|
|
if message.id not in conf[str(ctx.guild.id)]["reaction_messages"]: # Check if message is set
|
|
|
|
raise commands.BadArgument("not set")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
2019-08-10 17:42:14 +02:00
|
|
|
if action == "add": # Add reaction
|
|
|
|
if len(ctx.message.role_mentions) != 1: # Check if correct mentioned role
|
|
|
|
raise commands.BadArgument("role")
|
2019-08-10 16:07:53 +02:00
|
|
|
else:
|
2019-08-10 17:42:14 +02:00
|
|
|
try: # Add reaction to message and check if given emoji is correct
|
2019-08-10 16:07:53 +02:00
|
|
|
await message.add_reaction(reaction)
|
|
|
|
except InvalidArgument:
|
2019-08-10 17:42:14 +02:00
|
|
|
raise commands.BadArgument("reaction")
|
|
|
|
else: # Add reaction and role to configuration
|
2019-08-10 16:07:53 +02:00
|
|
|
conf[str(ctx.guild.id)]["reaction_messages"][message.id][reaction] =\
|
|
|
|
ctx.message.role_mentions[0].id
|
2019-08-10 17:42:14 +02:00
|
|
|
await ctx.send("Reaction add :white_check_mark:")
|
|
|
|
if action == "remove": # Remove a reaction
|
|
|
|
try: # Remove reaction from message and check if given emoji is correct
|
2019-08-10 16:07:53 +02:00
|
|
|
await message.remove_reaction(reaction, bot.user)
|
|
|
|
except (InvalidArgument, NotFound):
|
2019-08-10 17:42:14 +02:00
|
|
|
raise commands.BadArgument("reaction")
|
|
|
|
else: # Delete reaction from configuration
|
2019-08-10 16:07:53 +02:00
|
|
|
del conf[str(ctx.guild.id)]["reaction_messages"][message.id][reaction]
|
2019-08-10 17:42:14 +02:00
|
|
|
await ctx.send("Reaction remove :wastebasket:")
|
|
|
|
|
|
|
|
|
|
|
|
@reaction_message.error
|
|
|
|
async def reaction_message_error(ctx, error):
|
|
|
|
"""ctx: context object, error: raised error
|
|
|
|
Manage reaction_message command errors"""
|
|
|
|
err = {"argument": "Invalid arguments !", "message id": "Invalid message id", "already set": "Message already set",
|
|
|
|
"not set": "Message not set", "role": "Invalid mentioned role", "reaction": "Invalid reaction"} # Database
|
|
|
|
if str(error) in err:
|
|
|
|
await ctx.send(f"{err[str(error)]} :x:")
|
|
|
|
if isinstance(error, commands.errors.MissingPermissions):
|
|
|
|
await ctx.send("You are missing Administrator permission to run this command ! :no_entry:")
|
2019-08-10 16:07:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bot.command()
|
2019-08-10 17:42:14 +02:00
|
|
|
@commands.is_owner()
|
|
|
|
async def shutdown(ctx):
|
|
|
|
"""ctx: context object
|
|
|
|
Owner shutdown command"""
|
|
|
|
if await bot.is_owner(ctx.author):
|
|
|
|
await ctx.send("Shutdown ! :wave:")
|
|
|
|
await bot.logout()
|
|
|
|
await bot.close()
|
|
|
|
|
|
|
|
|
|
|
|
@shutdown.error
|
|
|
|
async def shutdown_error(ctx, error):
|
|
|
|
"""ctx: context object, error: raised error
|
|
|
|
Manage shutdown command errors"""
|
|
|
|
if isinstance(error, commands.errors.NotOwner):
|
|
|
|
await ctx.send("You do not own this bot ! :no_entry:")
|
|
|
|
|
|
|
|
|
|
|
|
@bot.command()
|
|
|
|
@commands.is_owner()
|
2019-08-10 16:07:53 +02:00
|
|
|
async def debug(ctx, config=None):
|
2019-08-10 17:42:14 +02:00
|
|
|
"""ctx: context object, config: (str) configuration entry to check"""
|
|
|
|
if await bot.is_owner(ctx.author):
|
|
|
|
with shelve.open("config.conf") as conf:
|
|
|
|
data = conf[str(ctx.guild.id)] # Add all guild configuration
|
|
|
|
if config: # If specified send only target configuration
|
|
|
|
data = data[config]
|
|
|
|
await ctx.send(f"``{data}``")
|
|
|
|
|
|
|
|
|
|
|
|
@debug.error
|
|
|
|
async def debug_error(ctx, error):
|
|
|
|
"""ctx: context object, error: raised error
|
|
|
|
Manage debug command errors"""
|
|
|
|
if isinstance(error, commands.errors.NotOwner):
|
|
|
|
await ctx.send("You do not own this bot ! :no_entry:")
|
|
|
|
|
2019-08-10 16:07:53 +02:00
|
|
|
|
2019-08-10 17:42:14 +02:00
|
|
|
bot.run(open("token.ini").read()) # Get token from token.ini and start the bot
|