-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmoderation.py
163 lines (129 loc) · 6.37 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
152
153
154
155
156
157
158
159
160
161
162
163
import os
import discord
from discord import utils
from discord.ext import commands
import embed
from .data import Data
db = Data() # Initialize database
def prepare_action(ctx, name: str):
role = utils.get(ctx.guild.roles, name=name)
# Create default embed
emb = embed.footer_embed(ctx.message, 'Moderation')
channel = ctx.message.channel
return role, emb, channel
def process_action(ctx, title, emb, member: discord.Member, msg, duration=None):
emb.add_field(name=title, value=f'{member.mention} ({member.id})', inline=False)
if duration:
emb.add_field(name='Duration', value=f'{duration}', inline=False)
emb.add_field(name="Reason", value=f'{msg}', inline=False)
emb.add_field(name="Moderator",
value=ctx.message.author.mention,
inline=False)
# Add data to database
# (Action, Duration, Reason, Moderator, Target, Date)
db.modEntry.insert(title, duration, msg, ctx.message.author.id, member.id,
ctx.message.created_at)
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.log_ch: discord.TextChannel = self.bot.get_channel(int(os.getenv("MODERATION_LOGS_CHANNEL")))
@commands.command()
@commands.has_role("Moderator")
async def ping_library_developers(self, ctx, title, *, message):
role = utils.get(ctx.guild.roles, name="Library Developer")
await role.edit(mentionable=True)
await ctx.send(f'{role.mention}\n**{title}**\n{message}')
await role.edit(mentionable=False)
@commands.command()
@commands.has_any_role("Library Developer", "Moderator")
async def mute(self, ctx, member: discord.Member, duration, *, message):
role, emb, channel = prepare_action(ctx, 'Muted')
# If Moderator invokes, mute from server and not just channel
if utils.get(ctx.guild.roles, name='Moderator') in ctx.message.author.roles:
process_action(ctx, 'Mute (Server)', emb, member, message, duration)
await member.add_roles(role)
# If Library Developer invokes, mute from channel and not server
if ((utils.get(ctx.guild.roles, name="Library Developer") in ctx.message.author.roles) and (
channel.category.name == "libraries" or channel.category.name == "frameworks")):
process_action(ctx, 'Mute (Channel)', emb, member, 'n/a')
await channel.set_permissions(member, read_messages=True, send_messages=False)
await ctx.message.add_reaction('👍')
await self.log_ch.send(embed=emb)
@commands.command()
@commands.has_any_role("Library Developer", "Moderator")
async def unmute(self, ctx, member: discord.Member, *, message):
role, emb, channel = prepare_action(ctx, 'Muted')
# If Moderator invokes, unmute from server and not just channel
if utils.get(ctx.guild.roles, name="Moderator") in ctx.message.author.roles:
process_action(ctx, 'Unmute (Server)', emb, member, message)
await member.remove_roles(role)
# If Library Developer invokes, unmute from channel and not server
elif ((utils.get(ctx.guild.roles, name="Library Developer") in ctx.message.author.roles) and (
channel.category.name == "libraries" or channel.category.name == "frameworks")):
process_action(ctx, 'Unmute (Channel)', emb, member, message)
await channel.set_permissions(member, overwrite=None)
await ctx.message.add_reaction('👍')
await self.log_ch.send(embed=emb)
@commands.command()
@commands.has_role("Moderator")
async def warn(self, ctx, member: discord.Member, *, message):
_, emb, channel = prepare_action(ctx, '')
process_action(ctx, 'Warn', emb, member, message)
await ctx.message.add_reaction('👍')
await self.log_ch.send(embed=emb)
@commands.command()
@commands.has_any_role("Library Developer", "Moderator")
async def infractions(self, ctx, member: discord.Member):
i = 0
# Fetch data from database
query = db.modEntry.fetch(member.id)
# If no data exists for user, abort
if len(query) == 0:
await ctx.send(f'User "{member.id}" has no infractions.')
return
_, emb, _ = prepare_action(ctx, '')
for case in query:
emb.add_field(name=f'Case Number: {case["index"]}',
value=f'Action: {case["action"]}\nDuration: {case["duration"]}\nUser: {case["target"]}\n'
f'Reason: {case["reason"]}\nModerator: {case["moderator"]}\n Date: {case["date"]}',
inline=False)
if i >= 25:
await ctx.send(embed=emb)
i = 0
i += 1
await ctx.send(embed=emb)
@commands.command()
@commands.has_role("Moderator")
async def reason(self, ctx, id, *, message):
sent = db.modEntry.update(id, reason=message)
if sent:
await ctx.send(f'Case Number "{id}" was updated successfully.')
return
await ctx.send(f'Case Number "{id}" failed to update.')
@commands.command()
@commands.has_role("Moderator")
async def duration(self, ctx, id, *, _duration):
sent = db.modEntry.update(id, _duration=_duration)
if sent:
await ctx.send(f'Case Number "{id}" was updated successfully.')
return
await ctx.send(f'Case Number "{id}" failed to update.')
@commands.command()
@commands.has_role("Moderator")
async def clean(self, ctx, amount: int, member: discord.Member = None):
_, emb, ch = prepare_action(ctx, '')
emb.add_field(name='Message (Clean)', value=ch.mention, inline=False)
if member:
emb.add_field(name="From", value=member.mention, inline=False)
def is_member(message):
return message.author.id == member.id
msgs = await ctx.message.channel.purge(limit=amount + 1, check=is_member)
amount = len(msgs) - 1
# If specific member is not specified
else:
await ctx.message.channel.purge(limit=amount + 1)
emb.insert_field_at(1, name="Amount", value=str(amount), inline=False)
emb.add_field(name="Moderator", value=ctx.message.author.mention, inline=False)
await self.log_ch.send(embed=emb)
def setup(bot):
bot.add_cog(Moderation(bot))