|
| 1 | +import asyncio |
| 2 | +from datetime import datetime |
| 3 | +from typing import Optional, List |
| 4 | + |
| 5 | +import discord |
| 6 | +from discord import SlashCommandOption as Option, Localizations, Permissions, ApplicationCommandInteraction as APPCI |
| 7 | +from discord.ext import commands |
| 8 | + |
| 9 | + |
| 10 | +# The examples for using localizations are german in this case because it is my native language. Of curse you can add your native language too |
| 11 | +class ClearCommand(commands.Cog): |
| 12 | + def __init__(self, bot): |
| 13 | + self.bot = bot |
| 14 | + |
| 15 | + @commands.Cog.slash_command( |
| 16 | + name='clear', |
| 17 | + description='Deletes the last x messages in the chat.', |
| 18 | + description_localizations=Localizations(german='Löscht die letzten x Nachrichten im Chat.'), |
| 19 | + default_required_permissions=Permissions(manage_messages=True), |
| 20 | + options=[ |
| 21 | + Option( |
| 22 | + option_type=int, |
| 23 | + name='count', |
| 24 | + description='The number (x) of messages that should be deleted.', |
| 25 | + description_localizations=Localizations(german='Wie viele (x) Nachrichten gelöscht werden sollen.'), |
| 26 | + max_value=100, |
| 27 | + min_value=2 |
| 28 | + ), |
| 29 | + Option( |
| 30 | + option_type=discord.Member, |
| 31 | + name='member', |
| 32 | + description='The member wich last x messages should be deleted.', |
| 33 | + description_localizations=Localizations( |
| 34 | + german='Der Member dessen letzten x Nachrichten gelöscht werden sollen.'), |
| 35 | + required=False |
| 36 | + ), |
| 37 | + Option( |
| 38 | + option_type=str, |
| 39 | + name='members', |
| 40 | + description='Separated by a , the member wich last x messages should be deleted. Separated by a ,', |
| 41 | + description_localizations=Localizations( |
| 42 | + german='Per , getrennt, die Member deren letzten x Nachrichten gelöscht werden sollen.'), |
| 43 | + required=False, |
| 44 | + converter=commands.Greedy[discord.Member] |
| 45 | + ), |
| 46 | + Option( |
| 47 | + option_type=str, |
| 48 | + name='before', |
| 49 | + name_localizations=Localizations(german='vor'), |
| 50 | + description='If specified, the last x messages before this message are deleted.', |
| 51 | + description_localizations=Localizations( |
| 52 | + german='Wenn angegeben werden die letzten x Nachrichten nach dieser Nachricht gelöscht.' |
| 53 | + ), |
| 54 | + required=False, |
| 55 | + converter=commands.MessageConverter |
| 56 | + ), |
| 57 | + Option( |
| 58 | + option_type=str, |
| 59 | + name='after', |
| 60 | + name_localizations=Localizations(german='nach'), |
| 61 | + description='If specified, the last x messages after this message are deleted.', |
| 62 | + description_localizations=Localizations( |
| 63 | + german='Wenn angegeben werden die letzten x Nachrichten vor dieser Nachricht gelöscht.' |
| 64 | + ), |
| 65 | + required=False, |
| 66 | + converter=commands.MessageConverter |
| 67 | + ), |
| 68 | + Option( |
| 69 | + option_type=str, |
| 70 | + name='reason', |
| 71 | + name_localizations=Localizations(german='grund'), |
| 72 | + description='The reason for deleting message are deleted.', |
| 73 | + description_localizations=Localizations( |
| 74 | + german='Der Grund warum die Nachrichten gelöscht werden.' |
| 75 | + ), |
| 76 | + required=False, |
| 77 | + ) |
| 78 | +
|
| 79 | + ], |
| 80 | + guild_ids=[977326610334228502]) |
| 81 | + async def clear(self, |
| 82 | + ctx: APPCI, |
| 83 | + count: int, |
| 84 | + member: discord.Member = None, |
| 85 | + members: Optional[List[discord.Member]] = [], |
| 86 | + before: discord.Message = None, |
| 87 | + after: discord.Message = None, |
| 88 | + reason: str = None, |
| 89 | + around: discord.Message = None): |
| 90 | + if not ctx.author.permissions_in(ctx.channel).manage_messages: |
| 91 | + return |
| 92 | + if members and len(members) == 1 and member == members[0]: |
| 93 | + member = members[0] |
| 94 | + question = await ctx.respond( |
| 95 | + embed=discord.Embed( |
| 96 | + title=f"deleting messages", |
| 97 | + description=f'[{ctx.author.mention}]\n are you sure to delete the' |
| 98 | + f' last {count} messages {f"from {member.mention}" if member else (f"{len(members)} members" if members else "in this Channel")}?', |
| 99 | + color=discord.Color.red()), |
| 100 | + components=[ |
| 101 | + [ |
| 102 | + discord.Button( |
| 103 | + label="Yes", |
| 104 | + emoji="✅", |
| 105 | + style=discord.ButtonColor.green, |
| 106 | + custom_id="Yes" |
| 107 | + ), |
| 108 | + discord.Button( |
| 109 | + label="No", |
| 110 | + emoji="❌", |
| 111 | + style=discord.ButtonColor.red, |
| 112 | + custom_id="No" |
| 113 | + ) |
| 114 | + ] |
| 115 | + ] |
| 116 | + ) |
| 117 | + |
| 118 | + def check(i: discord.ComponentInteraction, b: discord.Button): |
| 119 | + return i.message.id == question.id and i.author.id == ctx.author.id |
| 120 | + |
| 121 | + try: |
| 122 | + i, b = await self.bot.wait_for("button_click", check=check, timeout=10) |
| 123 | + await i.defer() |
| 124 | + if b.custom_id == 'Yes': |
| 125 | + await question.delete() |
| 126 | + |
| 127 | + def check(m: discord.Message): |
| 128 | + if member: |
| 129 | + if m.author.id == member.id: |
| 130 | + return True |
| 131 | + else: |
| 132 | + if members: |
| 133 | + if m.author in members: |
| 134 | + return True |
| 135 | + else: |
| 136 | + return True |
| 137 | + |
| 138 | + deleted = await ctx.channel.purge(limit=count, check=check, before=before, after=after, |
| 139 | + around=around) |
| 140 | + deleted_from = [] |
| 141 | + for m in deleted: |
| 142 | + if m.author not in deleted_from: |
| 143 | + deleted_from.append(m.author) |
| 144 | + m_count = len(deleted_from) |
| 145 | + embed = discord.Embed( |
| 146 | + description=f'Deleted `{len(deleted)}` messages from {m_count} member{"s" if m_count > 1 else ""}| {discord.utils.styled_timestamp(datetime.now(), "R")}', |
| 147 | + color=discord.Color.green() |
| 148 | + ) |
| 149 | + if reason: |
| 150 | + embed.add_field(name='Reason:', value=f'```\n{reason}\n```', inline=False) |
| 151 | + embed.add_field( |
| 152 | + name='Deleted messages from:', |
| 153 | + value=', '.join([m.mention for m in deleted_from]) |
| 154 | + ) |
| 155 | + await i.channel.send(embed=embed) |
| 156 | + else: |
| 157 | + await question.delete() |
| 158 | + except asyncio.TimeoutError: |
| 159 | + await question.delete() |
| 160 | + |
| 161 | + |
| 162 | +def setup(bot): |
| 163 | + bot.add_cog(ClearCommand(bot)) |
0 commit comments