1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
import discord
from discord.ext import commands, tasks
import datetime
from discord import app_commands
from global_variables import CONNECTION, BOT_COLOR
class Mute(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
def cog_load(self):
self.mute_check.start()
@tasks.loop(seconds=60)
async def mute_check(self):
cur = CONNECTION.cursor()
cur.execute("SELECT * FROM tempmute WHERE time < %s", (datetime.datetime.now(),))
data = cur.fetchall()
for row in data:
try:
guild_id = row[0]
user_id = row[1]
role_id = row[2]
guild = await self.bot.fetch_guild(guild_id)
user = await guild.fetch_member(user_id)
role = guild.get_role(role_id)
await user.remove_roles(role)
cur.execute("DELETE FROM tempmute WHERE guild_id = %s AND user_id = %s", (guild_id, user_id))
CONNECTION.commit()
except:
pass
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
@app_commands.describe(role_name='Name of your servers muted role')
async def setmute(
self,
interaction: discord.Interaction,
role_name: discord.Role
):
"Set the role for users to be given when muted"
guild_id = interaction.user.guild.id
role = role_name
role_id = role.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
role_id = cur.fetchone()
if role_id != None:
cur.execute("UPDATE mute SET role_id = %s WHERE guild_id = %s", (role.id, guild_id))
CONNECTION.commit()
else:
cur.execute("INSERT INTO mute (role_id, guild_id) VALUES(%s, %s)", (role.id, guild_id))
CONNECTION.commit()
embed = discord.Embed(
title = "Mute Role Set",
description = f"The mute role for {interaction.guild.name} has been set to: <@&{role.id}>",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed)
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
async def delmute(
self,
interaction: discord.Interaction
):
"Delete the role set to be given to muted users"
guild_id = interaction.user.guild.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
data = cur.fetchone()
if data:
cur.execute("DELETE FROM mute WHERE guild_id = %s", (guild_id,))
CONNECTION.commit()
embed = discord.Embed(
title = "Mute Role Deleted",
description = f"The mute role for {interaction.user.guild.name} has been deleted from my database.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed)
else:
embed = discord.Embed(
title = "Mute Role Not Set",
description = f"The mute role is not set, therefore there is no role I can delete.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
async def muterole(
self,
interaction: discord.Interaction
):
"See the current role set for when users are muted"
guild_id = interaction.user.guild.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
data = cur.fetchone()
if data:
role = interaction.guild.get_role(data[0])
if role:
embed = discord.Embed(
title=f"Mute Role for {interaction.user.guild.name}",
description=f"The role given to members who are muted in this server is: <@&{role.id}>",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
return await interaction.response.send_message(embed=embed)
else:
embed = discord.Embed(
title="Mute Role Not Found",
description=f"You have previously set a mute role, but it seems that the role you set has since been deleted. Please add a new mute role with the `/setmute` command.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
return await interaction.response.send_message(embed=embed)
else:
embed = discord.Embed(
title = "No Role Set",
description = f"It seems you haven't set a muted role yet. Please go do that with `/setmute` before running this command.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
@app_commands.describe(member='Name of the member you want to temporarily mute')
@app_commands.describe(time='Amount of time (in hours) to mute the member')
async def tempmute(
self,
interaction: discord.Interaction,
member: discord.Member,
time: app_commands.Range[int, 1, None]
):
"Mute a user for a specified amount of time"
try:
guild_id = interaction.user.guild.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
data = cur.fetchone()
role_id = data[0]
role_name = interaction.user.guild.get_role(role_id)
role = discord.utils.get(interaction.user.guild.roles, name=f"{role_name}")
await member.add_roles(role)
embed = discord.Embed(
title=f"Temporarily Muted {member}",
description=f"{interaction.user.mention} has temporarily muted {member.mention} for {time} hours.",
color=discord.Color.orange()
)
embed.set_thumbnail(url=member.avatar.url)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed)
cur = CONNECTION.cursor()
cur.execute("INSERT INTO tempmute (guild_id, user_id, role_id, time) VALUES (%s, %s, %s, %s)", (guild_id, member.id, role.id, (datetime.datetime.now() + datetime.timedelta(hours=time))))
CONNECTION.commit()
except TypeError:
embed = discord.Embed(
title = "No Role Set",
description = f"It seems you haven't set a muted role yet. Please go do that with `/setmute` before running this command.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
@app_commands.describe(member='Name of the member you want to mute')
@app_commands.describe(reason='Reason for muting the member')
async def mute(
self,
interaction: discord.Interaction,
member: discord.Member,
reason: str
):
"Mutes a user for an indefinite amount of time"
try:
guild_id = interaction.user.guild.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
data = cur.fetchone()
role_id = data[0]
role_name = interaction.user.guild.get_role(role_id)
role = discord.utils.get(interaction.user.guild.roles, name=f"{role_name}")
await member.add_roles(role)
embed = discord.Embed(
title=f"Muted {member}",
description=f"{interaction.user.mention} has successfully muted {member.mention} for `\"{reason}\"`.",
color=discord.Color.red()
)
embed.set_thumbnail(url=member.avatar.url)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed)
except TypeError or AttributeError:
embed = discord.Embed(
title = "No Role Set",
description = f"It seems you haven't set a muted role yet. Please go do that with `/setmute` before running this command.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
@app_commands.default_permissions(manage_roles=True)
@app_commands.command()
@app_commands.checks.has_permissions(manage_roles=True)
@app_commands.describe(member='Name of the member you want to unmute')
async def unmute(
self,
interaction: discord.Interaction,
member: discord.Member
):
"Unmute a specified member"
try:
guild_id = interaction.user.guild.id
cur = CONNECTION.cursor()
cur.execute("SELECT role_id FROM mute WHERE guild_id = %s", (guild_id,))
data = cur.fetchone()
role_id = data[0]
role_name = interaction.user.guild.get_role(role_id)
role = discord.utils.get(interaction.user.guild.roles, name=f"{role_name}")
if role in member.roles:
await member.remove_roles(role)
embed = discord.Embed(
title=f"Unmuted {member}",
description=f"{interaction.user.mention} has successfully unmuted {member.mention}.",
color=discord.Color.green()
)
embed.set_thumbnail(url=member.avatar.url)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed)
else:
embed = discord.Embed(
title="User Isn't Muted",
description=f"{member.mention} isn't muted, therefore I cannot unmute them. Maybe you meant to mute them with the `mute` command?",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
except TypeError or AttributeError:
embed = discord.Embed(
title="No Role Set",
description=f"It seems you haven't set a muted role yet. Please go do that with `/setmute` before running this command.",
color=BOT_COLOR
)
embed.set_footer(text=datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')+" UTC")
await interaction.response.send_message(embed=embed, ephemeral=True)
async def setup(bot):
await bot.add_cog(Mute(bot))
|