1
|
|
|
import asyncio |
2
|
|
|
import os |
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
import discord |
6
|
|
|
from discord.ext import commands |
7
|
|
|
from discord_slash import cog_ext, SlashContext |
8
|
|
|
|
9
|
|
|
import cogs.cogbase as cogbase |
10
|
|
|
from cogs.databasecog import DatabaseCog |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class AdminCog(cogbase.BaseCog): |
14
|
|
|
def __init__(self, base): |
15
|
|
|
super().__init__(base) |
16
|
|
|
|
17
|
|
|
# GENERAL FUNCTIONS |
18
|
|
|
# Check latency |
19
|
|
|
@cog_ext.cog_slash(name="ping", guild_ids=cogbase.GUILD_IDS, |
20
|
|
|
description="Check bot's latency", |
21
|
|
|
default_permission=False, |
22
|
|
|
permissions=cogbase.PERMISSION_MODS) |
23
|
|
|
async def check_ping(self, ctx: SlashContext) -> None: |
24
|
|
|
await ctx.send(f"Latency: {round(self.bot.latency * 1000)}ms", delete_after=4.0) |
25
|
|
|
|
26
|
|
|
# Clear messages |
27
|
|
|
@cog_ext.cog_slash(name="clear", guild_ids=cogbase.GUILD_IDS, |
28
|
|
|
description="Clear messages on channel", |
29
|
|
|
default_permission=False, |
30
|
|
|
permissions=cogbase.PERMISSION_MODS) |
31
|
|
|
async def purge_messages(self, ctx: SlashContext, number_to_delete: int = 1) -> None: |
32
|
|
|
messages = [] |
33
|
|
|
await ctx.send(f"Clearing {number_to_delete} messages!", delete_after=3) |
34
|
|
|
async for message in ctx.channel.history(limit=number_to_delete + 1): |
35
|
|
|
messages.append(message) |
36
|
|
|
await ctx.channel.delete_messages(messages) |
37
|
|
|
|
38
|
|
|
await asyncio.sleep(5) |
39
|
|
|
|
40
|
|
|
# Disconnect Bot |
41
|
|
|
@cog_ext.cog_slash(name="exit", guild_ids=cogbase.GUILD_IDS, |
42
|
|
|
description="Turn off the bot", |
43
|
|
|
default_permission=False, |
44
|
|
|
permissions=cogbase.PERMISSION_ADMINS) |
45
|
|
|
async def exit_bot(self, ctx: SlashContext) -> None: |
46
|
|
|
await ctx.send("Closing Bot", delete_after=1.0) |
47
|
|
|
self.create_log_msg("Exiting Bot") |
48
|
|
|
await asyncio.sleep(3) |
49
|
|
|
await self.bot.close() |
50
|
|
|
|
51
|
|
|
# WARN FUNCTIONS |
52
|
|
|
|
53
|
|
|
# Warn user |
54
|
|
|
@cog_ext.cog_slash(name="warn", guild_ids=cogbase.GUILD_IDS, |
55
|
|
|
description="Warn member", |
56
|
|
|
default_permission=False, |
57
|
|
|
permissions=cogbase.PERMISSION_MODS) |
58
|
|
|
async def warn_user(self, ctx: SlashContext, user: discord.User, reason: str) -> None: |
59
|
|
|
await DatabaseCog.db_add_warn(user.id, reason) |
60
|
|
|
await ctx.send( |
61
|
|
|
f"{user.mention} was warned for:\n> {reason}\n") |
62
|
|
|
|
63
|
|
|
# Get list of user's warns |
64
|
|
|
@cog_ext.cog_slash(name="warns", guild_ids=cogbase.GUILD_IDS, |
65
|
|
|
description="Get member warns", |
66
|
|
|
default_permission=False, |
67
|
|
|
permissions=cogbase.PERMISSION_MODS) |
68
|
|
|
async def user_warns(self, ctx: SlashContext, user: discord.User) -> None: |
69
|
|
|
warns, nr_of_warns = await DatabaseCog.db_get_warns(user.id) |
70
|
|
|
nl = "\n" |
71
|
|
|
message = f"**{user.name}** has been warned **{nr_of_warns}** times\n\n_Reasons_:\n" \ |
72
|
|
|
f"{nl.join(warns)}\n" |
73
|
|
|
await ctx.author.send(message) |
74
|
|
|
await ctx.send(f"{user.name} warns has been sent to DM", hidden=True) |
75
|
|
|
|
76
|
|
|
# Remove all member's warns |
77
|
|
|
@cog_ext.cog_slash(name="removeWarns", guild_ids=cogbase.GUILD_IDS, |
78
|
|
|
description="Remove all member's warns", |
79
|
|
|
default_permission=False, |
80
|
|
|
permissions=cogbase.PERMISSION_ADMINS) |
81
|
|
|
async def remove_warns(self, ctx: SlashContext, user: discord.User) -> None: |
82
|
|
|
await DatabaseCog.db_remove_warns(user.id) |
83
|
|
|
await ctx.send(f"{user.display_name}'s warns were deleted", hidden=True) |
84
|
|
|
|
85
|
|
|
# Mute member |
86
|
|
|
@cog_ext.cog_slash(name="mute", guild_ids=cogbase.GUILD_IDS, |
87
|
|
|
description="Mute member for x minutes", |
88
|
|
|
default_permission=False, |
89
|
|
|
permissions=cogbase.PERMISSION_MODS) |
90
|
|
|
async def mute_user(self, ctx: SlashContext, user: discord.User, mute_time: int, reason: str) -> None: |
91
|
|
|
duration: int = mute_time * 60 |
92
|
|
|
guild = ctx.guild |
93
|
|
|
muted = discord.utils.get(guild.roles, name="Muted") |
94
|
|
|
|
95
|
|
|
if not muted: |
96
|
|
|
muted = await guild.create_role(name="Muted") |
97
|
|
|
for channel in guild.channels: |
98
|
|
|
await channel.set_permissions(muted, speak=False, send_messages=False, read_message_history=True, |
99
|
|
|
read_messages=False) |
100
|
|
|
await user.add_roles(muted, reason=reason) |
101
|
|
|
await ctx.send(f"{user.mention} Was muted by {ctx.author.name} for {mute_time} min\n" |
102
|
|
|
f"Reason: {reason}") |
103
|
|
|
await asyncio.sleep(duration) |
104
|
|
|
await user.remove_roles(muted) |
105
|
|
|
await ctx.send(f"{user.mention}'s mute is over", delete_after=30) |
106
|
|
|
await self.bot.send_message(user, "Your mute is over") |
107
|
|
|
|
108
|
|
|
# KICK FUNCTIONS |
109
|
|
|
@staticmethod |
110
|
|
|
async def operation(ctx: SlashContext, user: discord.Member, operation_t: str, reason: str = None) -> None: |
111
|
|
|
if user == ctx.author: |
112
|
|
|
await ctx.send(f"{user.mention} You can't {operation_t} yourself", delete_after=5.0) |
113
|
|
|
if operation_t == "kick": |
114
|
|
|
await user.kick(reason=reason) |
115
|
|
|
elif operation_t == "ban": |
116
|
|
|
await user.ban(reason=reason) |
117
|
|
|
elif operation_t == "softban": |
118
|
|
|
await user.ban(reason=reason) |
119
|
|
|
await user.unban(reason=reason) |
120
|
|
|
reason_str: str = f"\nReason: {reason}" if reason else "" |
121
|
|
|
await ctx.send(f"{user} was {operation_t}ed{reason_str}") |
122
|
|
|
await user.send(f"You were {operation_t}ed from {ctx.guild.name}{reason_str}") |
123
|
|
|
|
124
|
|
|
# Kick |
125
|
|
|
@cog_ext.cog_slash(name="kick", guild_ids=cogbase.GUILD_IDS, |
126
|
|
|
description="Kicks member from the server", |
127
|
|
|
default_permission=False, |
128
|
|
|
permissions=cogbase.PERMISSION_MODS) |
129
|
|
|
async def kick(self, ctx: SlashContext, user: discord.Member, *, reason: str = None) -> None: |
130
|
|
|
operation_t: str = "kick" |
131
|
|
|
await self.operation(ctx, user, operation_t, reason) |
132
|
|
|
|
133
|
|
|
# Ban |
134
|
|
|
@cog_ext.cog_slash(name="ban", guild_ids=cogbase.GUILD_IDS, |
135
|
|
|
description="Ban member from the server", |
136
|
|
|
default_permission=False, |
137
|
|
|
permissions=cogbase.PERMISSION_ADMINS) |
138
|
|
|
async def ban(self, ctx, user: discord.Member, *, reason: str = None) -> None: |
139
|
|
|
operation_t: str = "ban" |
140
|
|
|
await self.operation(ctx, user, operation_t, reason) |
141
|
|
|
|
142
|
|
|
# Softban |
143
|
|
|
@cog_ext.cog_slash(name="softban", guild_ids=cogbase.GUILD_IDS, |
144
|
|
|
description="Ban and unban the user, so their messages are deleted", |
145
|
|
|
default_permission=False, |
146
|
|
|
permissions=cogbase.PERMISSION_MODS) |
147
|
|
|
async def softban(self, ctx, user: discord.Member, *, reason: str = None) -> None: |
148
|
|
|
operation_t: str = "softban" |
149
|
|
|
await self.operation(ctx, user, operation_t, reason) |
150
|
|
|
|
151
|
|
|
# OTHER |
152
|
|
|
# Slow mode |
153
|
|
|
@cog_ext.cog_slash(name="slowmode", guild_ids=cogbase.GUILD_IDS, |
154
|
|
|
description="Enable slowmode on current channel", |
155
|
|
|
default_permission=False, |
156
|
|
|
permissions=cogbase.PERMISSION_MODS) |
157
|
|
|
async def slowmode(self, ctx: SlashContext, seconds: int = 0) -> None: |
158
|
|
|
if seconds > 120: |
159
|
|
|
await ctx.send(":no_entry: Amount can't be over 120 seconds") |
160
|
|
|
elif seconds == 0: |
161
|
|
|
await ctx.channel.edit(slowmode_delay=seconds) |
162
|
|
|
a = await ctx.send("Slowmode is off for this channel") |
163
|
|
|
await a.add_reaction("a:redcard:871861842639716472") |
164
|
|
|
else: |
165
|
|
|
numofsecs = "second" if seconds == 1 else "seconds" |
166
|
|
|
await ctx.channel.edit(slowmode_delay=seconds) |
167
|
|
|
confirm = await ctx.send( |
168
|
|
|
f"{ctx.author.display_name} set the channel slow mode delay to `{seconds}` {numofsecs}\n" |
169
|
|
|
f"To turn this off use /slowmode") |
170
|
|
|
await confirm.add_reaction("a:ResidentWitcher:871872130021736519") |
171
|
|
|
|
172
|
|
|
# Restart bot |
173
|
|
|
@cog_ext.cog_slash(name="restartBot", guild_ids=cogbase.GUILD_IDS, |
174
|
|
|
description="Restart bot", |
175
|
|
|
default_permission=False, |
176
|
|
|
permissions=cogbase.PERMISSION_MODS) |
177
|
|
|
async def restart_bot(self, ctx: SlashContext): |
178
|
|
|
await ctx.send("Restarting bot") |
179
|
|
|
os.execv(sys.executable, ['python'] + sys.argv) |
180
|
|
|
self.create_log_msg("Bot restarted") |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def setup(bot: commands.Bot) -> None: |
184
|
|
|
bot.add_cog(AdminCog(bot)) |
185
|
|
|
|