async def on_reaction_add(self, reaction, user): if not user.bot: message_id = reaction.message.id channel_id = reaction.message.channel.id guild_id = reaction.message.guild.id message_data = run_file_format( "sql/find_message.sql", message_id=message_id, channel_id=channel_id ) chore_id = message_data[0]["chore_id"] chore_data = run_file_format( "sql/find_chore.sql", guild_id=guild_id, chore_id=chore_id )[0] asignee_id = chore_data["user_id"] creator_id = chore_data["creator"] if reaction.emoji == "✅" and user.id == asignee_id: await complete_chore(reaction, user.id, chore_id) elif reaction.emoji == "🗑️" and ( check_admin(user.id, reaction.message.guild.id) or creator_id == user.id ): run_file_format( "sql/remove_chore.sql", guild_id=guild_id, chore_id=chore_id ) else: return None await del_messages(self.bot, guild_id, chore_id)
async def manipulate_admin(self, ctx: Context, member: discord.Member, level: int): invoker_id = ctx.author.id guild_id = ctx.guild.id target_id = member.id check_user(guild_id, invoker_id) check_user(guild_id, target_id) invoker_details = run_file_format("sql/find_user.sql", user_id=invoker_id, guild_id=guild_id)[0] target_details = run_file_format("sql/find_user.sql", user_id=target_id, guild_id=guild_id)[0] invoker_admin = invoker_details["admin_level"] target_admin = invoker_details["admin_level"] if (invoker_admin >= 1 and invoker_admin >= target_admin and 0 <= level <= invoker_admin): run_file_format( "sql/set_admin.sql", guild_id=guild_id, user_id=target_id, admin_level=level, ) await ctx.message.add_reaction("✅") return True else: await ctx.message.add_reaction("❌") return False
async def send_chore_message(bot, ctx, guild_id, chore_id): chore_data = run_file_format( "sql/find_chore.sql", guild_id=guild_id, chore_id=chore_id ) if not chore_data: return None chore_data = chore_data[0] guild_id = chore_data["guild_id"] user_id = chore_data["user_id"] description = chore_data["description"] assigned_date = chore_data["assigned_date"] deadline = chore_data["deadline"] member = bot.get_guild(guild_id).get_member(user_id) if member is not None: username = member.display_name url = member.avatar_url else: username = "******" url = "" embed = discord.Embed(title=username, description=description) if deadline and deadline < datetime.datetime.now(): embed.colour = discord.Colour.red() embed.set_thumbnail(url=url) embed.set_footer(text=f"id {chore_id} created at {assigned_date}") msg = await ctx.send(embed=embed) await msg.add_reaction("✅") await msg.add_reaction("🗑️") kwargs = { "message_id": msg.id, "channel_id": msg.channel.id, "chore_id": chore_id, "creation_time": datetime.datetime.now().strftime("%Y-%m-%-d %H:%M:%S"), } run_file_format("sql/add_message.sql", **kwargs)
async def add_chore(self, ctx: Context, member: discord.Member, *message): check_user(ctx.guild.id, member.id) message_data = parse_message(message) if not message_data: await ctx.message.add_reaction("❓") return False else: description, parsed_time = message_data chore_id = run_file_format("sql/fetch_number_chores.sql", guild_id=ctx.guild.id) print(chore_id) if chore_id and not chore_id[0]["max_chore_id"]: chore_id = 1 else: chore_id = chore_id[0]["max_chore_id"] + 1 kwargs = { "user_id": member.id, "creator": ctx.author.id, "guild_id": ctx.guild.id, "description": description, "assigned_date": datetime.datetime.now(), "chore_id": chore_id, "deadline": parsed_time, } run_file_format("sql/add_chore.sql", **kwargs) await send_chore_message(ctx.bot, ctx, ctx.guild.id, chore_id) return True
async def complete_chore(ctx, invoker_id, chore_id): chore_data = run_file_format( "sql/find_incomplete_chore.sql", guild_id=ctx.message.guild.id, chore_id=chore_id, ) if not chore_data: await ctx.message.add_reaction("❓") return False asignee_id = chore_data[0]["user_id"] assigned_date = chore_data[0]["assigned_date"] completed_date = datetime.datetime.now() time_taken = int((completed_date - assigned_date).total_seconds()) if invoker_id == asignee_id: print("...match") kwargs = { "guild_id": ctx.message.guild.id, "completed_date": completed_date, "chore_id": chore_id, "time_taken": time_taken, } run_file_format("sql/complete_chore.sql", **kwargs) await ctx.message.add_reaction("✅") return True else: await ctx.message.add_reaction("❌") return False
def set_admin(guild_id, user_id, admin_level): check_user(guild_id, user_id) run_file_format( "sql/set_admin.sql", guild_id=guild_id, user_id=user_id, admin_level=admin_level, )
async def del_messages(bot, guild_id, chore_id): messages = run_file_format("sql/find_message_chore.sql", chore_id=chore_id) del_dict = {m["message_id"]: m["channel_id"] for m in messages} for m_id, c_id in del_dict.items(): channel = bot.get_channel(c_id) if channel: msg = await channel.fetch_message(m_id) await msg.delete() run_file_format("sql/delete_messages.sql", chore_id=chore_id)
def check_user(guild_id, user_id): hits = run_file_format("sql/find_user.sql", user_id=user_id, guild_id=guild_id) if not hits: run_file_format( "sql/add_user.sql", user_id=user_id, guild_id=guild_id, )
async def change_prefix(self, ctx, new_pre): if len(new_pre) != 1: await ctx.send(embed=discord.Embed( description="Prefixes must be length 1!", colour=discord.Colour.red(), )) else: run_file_format("sql/set_prefix.sql", guild_id=ctx.guild.id, prefix=new_pre) await ctx.message.add_reaction("✅")
async def on_ready(): print("Logged in as {0.name} ({0.id})".format(bot.user)) print("====================") print("Checking guilds...") guilds = bot.guilds owners = {g.id: g.owner.id for g in guilds} for g, o in owners.items(): set_admin(g, o, 2) guild_ids = {g.id for g in guilds} existing_guilds = {r["guild_id"] for r in run_file_format("sql/select_guilds.sql")} missing_guilds = guild_ids - existing_guilds for guild in missing_guilds: run_file_format("sql/add_guild.sql", guild_id=guild) print("...done") await bot.change_presence(activity=discord.Game(f"{prefix}help"))
async def remove_chore(self, ctx: Context, chore_id: int): chore_data = run_file_format("sql/find_chore.sql", guild_id=ctx.guild.id, chore_id=chore_id) if not chore_data: await ctx.message.add_reaction("❓") return False creator = chore_data[0]["creator"] if check_admin(ctx.author.id, ctx.guild.id) or creator == ctx.author.id: kwargs = {"guild_id": ctx.guild.id, "chore_id": chore_id} run_file_format("sql/remove_chore.sql", **kwargs) await ctx.message.add_reaction("✅") return True else: await ctx.message.add_reaction("❌") return False
def check_admin(user_id, guild_id, min_admin_level=1): check_user(guild_id, user_id) hits = run_file_format("sql/find_user.sql", user_id=user_id, guild_id=guild_id) print(hits) admin_level = hits[0]["admin_level"] if admin_level >= min_admin_level: return True else: return False
async def show_chores(self, ctx: Context, message): if message == "my": kwargs = {"guild_id": ctx.guild.id, "user_id": ctx.author.id} vals = run_file_format("sql/show_guild_user.sql", **kwargs) if not vals: embed = discord.Embed( description="You have no uncompleted chores!") await ctx.send(embed=embed) elif message == "all": kwargs = {"guild_id": ctx.guild.id} vals = run_file_format("sql/show_guild.sql", **kwargs) if not vals: embed = discord.Embed( description="There are no uncomplete chores on the server!" ) await ctx.send(embed=embed) else: return None for v in vals: await send_chore_message(ctx.bot, ctx, ctx.guild.id, v["chore_id"]) await ctx.message.delete()
async def assign_chore(self, ctx: Context, *message): message_data = parse_message(message) if not message_data: await ctx.message.add_reaction("❓") return False description, parsed_time = message_data potential = np.array([ x["user_id"] for x in run_file_format("sql/show_users.sql", guild_id=ctx.guild.id) ]) complete = np.array([ run_file_format("sql/count_complete.sql", guild_id=ctx.guild.id, user_id=x)[0]["complete_chores"] for x in potential ]) if len(potential) > 0 and len(complete) > 0: min_users = potential[complete == min(complete)] unlucky_sod = random.choice(min_users) else: unlucky_sod = ctx.message.author.id chore_id = run_file_format("sql/fetch_number_chores.sql", guild_id=ctx.guild.id) print(chore_id) if chore_id and not chore_id[0]["max_chore_id"]: chore_id = 1 else: chore_id = chore_id[0]["max_chore_id"] + 1 kwargs = { "user_id": unlucky_sod, "creator": ctx.author.id, "guild_id": ctx.guild.id, "description": description, "assigned_date": datetime.datetime.now(), "chore_id": chore_id, "deadline": parsed_time, } run_file_format("sql/add_chore.sql", **kwargs) await ctx.send(f"<@{unlucky_sod}> drew the short straw!") await send_chore_message(ctx.bot, ctx, ctx.guild.id, chore_id) return True
async def register(self, ctx: Context, *args): guild_id = ctx.guild.id if ctx.message.mentions: for member in ctx.message.mentions: target_id = member.id check_user(guild_id, target_id) run_file_format( "sql/set_registered.sql", guild_id=guild_id, user_id=target_id, registered=1, ) else: check_user(guild_id, ctx.message.author.id) run_file_format( "sql/set_registered.sql", guild_id=guild_id, user_id=ctx.message.author.id, registered=1, ) await ctx.message.add_reaction("✅") return True
def get_prefix(bot: Bot, message): prefix = run_file_format("sql/get_prefix.sql", guild_id=message.guild.id)[0][ "prefix" ] return commands.when_mentioned_or(prefix)(bot, message)