-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoderation.py
151 lines (125 loc) · 4.91 KB
/
moderation.py
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
import re
import datetime
from copy import deepcopy
import asyncio
import discord
from discord.ext import commands, tasks
from dateutil.relativedelta import relativedelta
time_regex = re.compile("(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}
class TimeConverter(commands.Converter):
async def convert(self, ctx, argument):
args = argument.lower()
matches = re.findall(time_regex, args)
time = 0
for key, value in matches:
try:
time += time_dict[value] * float(key)
except KeyError:
raise commands.BadArgument(
f"{value} is an invalid time key! h|m|s|d are valid arguments"
)
except ValueError:
raise commands.BadArgument(f"{key} is not a number!")
return round(time)
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.mute_task = self.check_current_mutes.start()
def cog_unload(self):
self.mute_task.cancel()
@tasks.loop(minutes=5)
async def check_current_mutes(self):
currentTime = datetime.datetime.now()
mutes = deepcopy(self.bot.muted_users)
for key, value in mutes.items():
if value["muteDuration"] is None:
continue
unmuteTime = value["mutedAt"] + relativedelta(seconds=value["muteDuration"])
if currentTime >= unmuteTime:
guild = self.bot.get_guild(value["guildId"])
member = guild.get_member(value["_id"])
role = discord.utils.get(guild.roles, name="Muted")
if role in member.roles:
await member.remove_roles(role)
print(f"Unmuted: {member.display_name}")
await self.bot.mutes.delete(member.id)
try:
self.bot.muted_users.pop(member.id)
except KeyError:
pass
@check_current_mutes.before_loop
async def before_check_current_mutes(self):
await self.bot.wait_until_ready()
@commands.command(
name="mute",
description="Mutes a given user for x time!",
ussage="<user> [time]",
)
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member: discord.Member, *, time: TimeConverter = None):
role = discord.utils.get(ctx.guild.roles, name="Muted")
if not role:
await ctx.send("No muted role was found! Please create one called `Muted`")
return
try:
if self.bot.muted_users[member.id]:
await ctx.send("This user is already muted")
return
except KeyError:
pass
data = {
"_id": member.id,
"mutedAt": datetime.datetime.now(),
"muteDuration": time or None,
"mutedBy": ctx.author.id,
"guildId": ctx.guild.id,
}
await self.bot.mutes.upsert(data)
self.bot.muted_users[member.id] = data
await member.add_roles(role)
if not time:
await ctx.send(f"Muted {member.display_name}")
else:
minutes, seconds = divmod(time, 60)
hours, minutes = divmod(minutes, 60)
if int(hours):
await ctx.send(
f"Muted {member.display_name} for {hours} hours, {minutes} minutes and {seconds} seconds"
)
elif int(minutes):
await ctx.send(
f"Muted {member.display_name} for {minutes} minutes and {seconds} seconds"
)
elif int(seconds):
await ctx.send(f"Muted {member.display_name} for {seconds} seconds")
print(type(time))
if time and time < 300:
await asyncio.sleep(time)
if role in member.roles:
await member.remove_roles(role)
await ctx.send(f"Unmuted `{member.display_name}`")
await self.bot.mutes.delete(member.id)
try:
self.bot.muted_users.pop(member.id)
except KeyError:
pass
@commands.command(name="unmute", description="Unmuted a member!", usage="<user>")
@commands.has_permissions(manage_roles=True)
async def unmute(self, ctx, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, name="Muted")
if not role:
await ctx.send("No muted role was found! Please create one called `Muted`")
return
await self.bot.mutes.delete(member.id)
try:
self.bot.muted_users.pop(member.id)
except KeyError:
pass
if role not in member.roles:
await ctx.send("This member is not muted.")
return
await member.remove_roles(role)
await ctx.send(f"Unmuted `{member.display_name}`")
def setup(bot):
bot.add_cog(Moderation(bot))