|
1 | 1 | import nextcord
|
2 |
| -from typing import Optional |
3 |
| -from nextcord.ext import commands |
| 2 | +from nextcord.ext import commands, application_checks |
4 | 3 | from nextcord import Member
|
5 | 4 |
|
| 5 | +import asyncio |
| 6 | + |
6 | 7 |
|
7 | 8 | class Moderation(commands.Cog):
|
8 | 9 | def __init__(self, client):
|
9 | 10 | self.client = client
|
10 |
| - |
11 |
| - # Closing channel |
12 |
| - |
13 |
| - @commands.command(aliases=["lock"]) |
14 |
| - @commands.has_permissions(manage_channels=True) |
15 |
| - async def lockdown(self, ctx): |
16 |
| - await ctx.message.delete() |
17 |
| - await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False) |
18 |
| - await ctx.send(ctx.channel.mention + " - **Channel locked.**") |
19 |
| - |
20 |
| - # Opening channel |
21 |
| - |
22 |
| - @commands.command() |
23 |
| - @commands.has_permissions(manage_channels=True) |
24 |
| - async def unlock(self, ctx): |
25 |
| - await ctx.message.delete() |
26 |
| - await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=True) |
27 |
| - await ctx.send( ctx.channel.mention + " - **Channel unclocked.**") |
28 |
| - |
29 |
| - # Ban |
30 |
| - |
31 |
| - @commands.command() |
32 |
| - @commands.has_permissions(ban_members=True) |
33 |
| - async def ban(self, ctx, member: Member, *, reason: Optional[str] = "No reason."): |
34 |
| - await ctx.message.delete() |
35 |
| - await ctx.channel.trigger_typing() |
36 |
| - embed = nextcord.Embed(description=f"🚫 User **{member.name}** banned!", colour=0xff0000) |
37 |
| - embed.add_field(name="User", value=f"{member.mention}") |
38 |
| - embed.add_field(name="Reason", value=f"{reason}") |
39 |
| - embed.set_footer(text=f'Administrator - {ctx.author.name}#{ctx.author.discriminator}', icon_url=ctx.author.avatar.url) |
40 |
| - await ctx.send(embed=embed) |
41 |
| - await member.ban(reason=reason) |
42 |
| - |
43 |
| - # Unban |
44 |
| - |
45 |
| - @commands.command() |
46 |
| - @commands.has_permissions(administrator=True) |
47 |
| - async def unban(self, ctx, *, member, reason: Optional[str] = "No reason."): |
48 |
| - await ctx.message.delete() |
49 |
| - await ctx.channel.trigger_typing() |
50 |
| - banned_users = await ctx.guild.bans() |
51 |
| - member_name, member_discriminator = member.split("#") |
52 |
| - |
53 |
| - for ban_entry in banned_users: |
54 |
| - user = ban_entry.user |
55 |
| - |
56 |
| - if(user.name, user.discriminator) == (member_name, member_discriminator): |
57 |
| - await ctx.channel.trigger_typing() |
58 |
| - await ctx.guild.unban(user) |
59 |
| - embed = nextcord.Embed(description=f"✅ User **{user.name}** unbanned!", colour=0x33ff00) |
60 |
| - embed.add_field(name="User", value=f"{user.mention}") |
61 |
| - embed.add_field(name="Reason", value=f"{reason}") |
62 |
| - embed.set_footer(text=f'Administrator - {ctx.author.name}#{ctx.author.discriminator}', icon_url=ctx.author.avatar.url) |
63 |
| - await ctx.send(embed=embed) |
64 |
| - return |
65 |
| - |
66 |
| - # Kick |
67 |
| - |
68 |
| - @commands.command() |
69 |
| - @commands.has_permissions(kick_members=True) |
70 |
| - async def kick(self, ctx, member: nextcord.Member, *, reason: Optional[str] = "No reason."): |
71 |
| - await ctx.message.delete() |
72 |
| - await ctx.channel.trigger_typing() |
73 |
| - await member.kick(reason=reason) |
74 |
| - |
75 |
| - embed = nextcord.Embed(description=f"❌ User **{member.name}** kicked!", colour=0xff0000) |
76 |
| - embed.add_field(name="User", value=f"{member.mention}", inline=True) |
77 |
| - embed.add_field(name="Reason", value=f"{reason}", inline=True) |
78 |
| - embed.set_footer(text=f'Administrator - {ctx.author.name}#{ctx.author.discriminator}', icon_url=ctx.author.avatar.url) |
79 |
| - await ctx.send(embed=embed) |
80 | 11 |
|
81 |
| - # Clear / Purge |
| 12 | + @application_checks.has_permissions(manage_channels=True) |
| 13 | + @nextcord.slash_command() |
| 14 | + async def lockdown(self, interaction): |
| 15 | + """ |
| 16 | + Lock a channel. |
| 17 | + """ |
| 18 | + current_permissions = interaction.channel.overwrites_for(interaction.guild.default_role) |
| 19 | + current_permissions.send_messages = False |
| 20 | + await interaction.channel.set_permissions(interaction.guild.default_role, overwrite=current_permissions) |
| 21 | + embed = nextcord.Embed(description=f"**🔒 Channel locked**\n\n{interaction.channel.mention}") |
| 22 | + embed.set_author(name=interaction.user.name, icon_url=interaction.user.avatar.url) |
| 23 | + |
| 24 | + await interaction.send(embed=embed) |
| 25 | + |
| 26 | + @application_checks.has_permissions(manage_channels=True) |
| 27 | + @nextcord.slash_command() |
| 28 | + async def unlock(self, interaction): |
| 29 | + """ |
| 30 | + Open a channel. |
| 31 | + """ |
| 32 | + embed = nextcord.Embed(title="Channel Unlocked", description=interaction.channel.mention, |
| 33 | + colour=nextcord.Color.brand_green()) |
| 34 | + embed.set_author(name=interaction.user.name, icon_url=interaction.user.avatar.url) |
| 35 | + |
| 36 | + current_permissions = interaction.channel.overwrites_for(interaction.guild.default_role) |
| 37 | + |
| 38 | + if current_permissions.send_messages is True: |
| 39 | + return await interaction.send("**You cannot unlock a channel that is not locked!**", |
| 40 | + ephemeral=True) |
| 41 | + |
| 42 | + current_permissions.send_messages = True |
| 43 | + await interaction.channel.set_permissions(interaction.guild.default_role, overwrite=current_permissions) |
| 44 | + |
| 45 | + await interaction.send(embed=embed) |
| 46 | + |
| 47 | + @application_checks.has_permissions(ban_members=True) |
| 48 | + @nextcord.slash_command() |
| 49 | + async def ban(self, interaction, member: Member, reason: str = nextcord.SlashOption(default="No reason.")): |
| 50 | + """ |
| 51 | + Ban a certain user. |
| 52 | + """ |
| 53 | + embed = nextcord.Embed(title='Ban', colour=nextcord.Colors.light_red) |
| 54 | + |
| 55 | + error_embed = nextcord.Embed(title="Error", colour=nextcord.Colors.light_red) |
| 56 | + |
| 57 | + if member.bot: |
| 58 | + error_embed.description = "**You cannot ban a bot!**" |
| 59 | + return await interaction.send(embed=error_embed, ephemeral=True) |
| 60 | + |
| 61 | + elif interaction.user.id == member.id: |
| 62 | + error_embed.description = "**You cannot ban yourself!**" |
| 63 | + return await interaction.send(embed=error_embed, ephemeral=True) |
| 64 | + |
| 65 | + else: |
| 66 | + embed.description = f"🚫 User **{member.name}** has been banned!" |
| 67 | + embed.add_field(name="User", value=member.mention) |
| 68 | + embed.add_field(name="Reason", value=reason) |
| 69 | + embed.set_footer(text=f'Administrator - {interaction.user.name}', |
| 70 | + icon_url=interaction.user.avatar.url) |
| 71 | + await member.ban(reason=reason) |
| 72 | + await interaction.send(embed=embed) |
| 73 | + |
| 74 | + @application_checks.has_permissions(administrator=True) |
| 75 | + @nextcord.slash_command() |
| 76 | + async def unban(self, interaction, member, reason: str = nextcord.SlashOption(default="No reason.")): |
| 77 | + banned_users = await interaction.guild.fetch_ban(user=member) |
| 78 | + |
| 79 | + embed = nextcord.Embed(title='Unban') |
| 80 | + |
| 81 | + if interaction.user.id == member.id: |
| 82 | + embed.title = 'Error' |
| 83 | + embed.description = "You cannot unban yourself!" |
| 84 | + embed.colour = nextcord.Color.red() |
| 85 | + return await interaction.send(embed=embed, ephemeral=True) |
| 86 | + |
| 87 | + if member.bot: |
| 88 | + embed.title = 'Error' |
| 89 | + embed.description = "Cannot unban the user. User is a bot." |
| 90 | + embed.colour = nextcord.Color.red() |
| 91 | + return await interaction.send(embed=embed, ephemeral=True) |
| 92 | + |
| 93 | + if banned_users: |
| 94 | + await interaction.guild.unban(member) |
| 95 | + |
| 96 | + embed.description = f"✅ User **{member.name}** has been unbanned!" |
| 97 | + embed.colour = nextcord.Color.green() |
| 98 | + |
| 99 | + embed.add_field(name="User", value=f"{member.mention}") |
| 100 | + embed.add_field(name="Reason", value=f"{reason}") |
| 101 | + embed.set_footer(text=f'Administrator - {interaction.user.name}', |
| 102 | + icon_url=interaction.user.avatar.url) |
| 103 | + await interaction.send(embed=embed) |
| 104 | + else: |
| 105 | + embed.description = "❌ The specified user is not banned." |
| 106 | + embed.colour = nextcord.Color.fuchsia() |
| 107 | + await interaction.send(embed=embed, ephemeral=True) |
| 108 | + |
| 109 | + @application_checks.has_permissions(kick_members=True) |
| 110 | + @nextcord.slash_command() |
| 111 | + async def kick(self, interaction, member: nextcord.Member, reason: str = nextcord.SlashOption(default="No reason.")): |
| 112 | + embed = nextcord.Embed(title='Kick') |
| 113 | + |
| 114 | + if interaction.user.id == member.id: |
| 115 | + embed.title = 'Error' |
| 116 | + embed.description = "You cannot kick yourself!" |
| 117 | + embed.colour = nextcord.Color.red() |
| 118 | + return await interaction.send(embed=embed, ephemeral=True) |
| 119 | + |
| 120 | + if member.bot: |
| 121 | + embed.title = 'Error' |
| 122 | + embed.description = "Cannot kick the user. User is a bot." |
| 123 | + embed.colour = nextcord.Color.red() |
| 124 | + return await interaction.send(embed=embed, ephemeral=True) |
82 | 125 |
|
83 |
| - @commands.command(aliases=["purge"]) |
84 |
| - @commands.has_permissions(manage_messages=True) |
85 |
| - async def clear(self, ctx, *, amount: int = None): |
86 |
| - try: |
87 |
| - await ctx.channel.purge(limit=amount+1) |
88 |
| - await ctx.send(f'{amount} messages deleted.') |
| 126 | + await member.kick(reason=reason) |
89 | 127 |
|
90 |
| - except Exception as error: |
91 |
| - await ctx.send(error) |
| 128 | + embed.description = f"❌ User **{member.name}** has been kicked!" |
| 129 | + embed.colour = nextcord.Color.yellow() |
92 | 130 |
|
93 |
| - except PermissionError: |
94 |
| - await ctx.send("You haven`t permissions.") |
| 131 | + embed.add_field(name="User", value=f"{member.mention}", inline=True) |
| 132 | + embed.add_field(name="Reason", value="Not specified." if not reason else reason, inline=True) |
| 133 | + embed.set_footer(text=f'Administrator - {interaction.user.name}', icon_url=interaction.user.avatar.url) |
| 134 | + await interaction.send(embed=embed) |
| 135 | + |
| 136 | + @application_checks.has_permissions(manage_messages=True) |
| 137 | + @nextcord.slash_command() |
| 138 | + async def clear(self, interaction, amount: int = nextcord.SlashOption(min_value=1, max_value=100)): |
| 139 | + deleted = await interaction.channel.purge(limit=amount) |
| 140 | + |
| 141 | + msg = await interaction.send(f'Messages deleted: **{len(deleted)}**') |
| 142 | + await asyncio.sleep(3) |
| 143 | + await msg.delete() |
95 | 144 |
|
96 | 145 |
|
97 | 146 | def setup(client):
|
|
0 commit comments