|
| 1 | +import logging |
| 2 | +import discord |
| 3 | +from discord.ext import commands |
| 4 | +import qna.classes |
| 5 | +from cogs.config import Config |
| 6 | + |
| 7 | + |
| 8 | +class ModMode(commands.Cog): |
| 9 | + def __init__(self, client: commands.Bot): |
| 10 | + self.client = client |
| 11 | + self.logger = logging.getLogger("cogs.ModMode") |
| 12 | + self.logger.debug("registered.") |
| 13 | + self.data = {} |
| 14 | + |
| 15 | + def is_in_mod_mode(self, guild: discord.Guild, user: discord.Member): |
| 16 | + return f"{guild.id}+{user.id}" in self.data |
| 17 | + |
| 18 | + def save_info(self, guild: discord.Guild, user: discord.Member, message: discord.Message, |
| 19 | + question: qna.classes.Question, response: qna.classes.Response): |
| 20 | + self.data[f"{guild.id}+{user.id}"][message.id] = {"question": question, "response": response} |
| 21 | + self.logger.debug("[%s] (%i) %s -> %s", str(user), message.id, question.text, response.text) |
| 22 | + |
| 23 | + @commands.has_permissions(manage_messages=True) |
| 24 | + @commands.hybrid_command(brief="Toggles moderator mode.") |
| 25 | + async def mod(self, ctx: commands.Context): |
| 26 | + if not self.is_in_mod_mode(ctx.guild, ctx.author): |
| 27 | + self.data[f"{ctx.guild.id}+{ctx.author.id}"] = {} |
| 28 | + await ctx.reply("You have entered **moderator mode**. Whenever bob replies to your messages, " |
| 29 | + "you will be able to delete its replies from the database.") |
| 30 | + else: |
| 31 | + del self.data[f"{ctx.guild.id}+{ctx.author.id}"] |
| 32 | + await ctx.reply("You have left moderator mode.") |
| 33 | + |
| 34 | + |
| 35 | +class DeleteView(discord.ui.View): |
| 36 | + def __init__(self, mod_mode: ModMode, config: Config): |
| 37 | + super().__init__() |
| 38 | + self.mod_mode = mod_mode |
| 39 | + self.config = config |
| 40 | + |
| 41 | + @discord.ui.button(label="Delete", style=discord.ButtonStyle.red) |
| 42 | + async def delete(self, interaction: discord.Interaction, _: discord.ui.Button): |
| 43 | + message = interaction.message |
| 44 | + guild = interaction.guild |
| 45 | + user = interaction.user |
| 46 | + if not self.mod_mode.is_in_mod_mode(guild, user): |
| 47 | + return await interaction.response.send_message("You are not in moderator mode.", ephemeral=True) |
| 48 | + print(message.id, self.mod_mode.data) |
| 49 | + if message.id in self.mod_mode.data[f"{guild.id}+{user.id}"]: |
| 50 | + question: qna.classes.Question = self.mod_mode.data[f"{guild.id}+{user.id}"][message.id]["question"] |
| 51 | + response: qna.classes.Response = self.mod_mode.data[f"{guild.id}+{user.id}"][message.id]["response"] |
| 52 | + question.responses.remove(response) |
| 53 | + self.mod_mode.logger.debug("Deleted response %s -> %s", question.text, response.text) |
| 54 | + |
| 55 | + if len(question.responses) == 0: |
| 56 | + del self.config.question_map[question.text + str(question.guild)] |
| 57 | + self.mod_mode.logger.debug("Deleted question %s", question.text) |
| 58 | + |
| 59 | + del self.mod_mode.data[f"{guild.id}+{user.id}"][message.id] |
| 60 | + |
| 61 | + await message.delete() |
| 62 | + await interaction.response.send_message("Deleted this reply from bob's database.", ephemeral=True) |
| 63 | + else: |
| 64 | + return await interaction.response.send_message("You're able to delete replies that only you generated.", |
| 65 | + ephemeral=True) |
| 66 | + |
| 67 | + |
| 68 | +async def setup(client: commands.Bot): |
| 69 | + await client.add_cog(ModMode(client)) |
0 commit comments