This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
/
utils.py
213 lines (181 loc) · 6.13 KB
/
utils.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
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
from datetime import datetime, timezone
from enum import Enum
from typing import Dict, List
from discord import Client, Color, Embed, Guild, Member, User
from discord.errors import Forbidden, NotFound
from discord_slash.model import ButtonStyle, SlashCommandPermissionType
from discord_slash.utils.manage_commands import create_permission
from discord_slash.utils.manage_components import (
create_actionrow,
create_button,
create_select,
create_select_option,
)
from loguru import logger
from config import CONFIG
from database import Block
REASONS_DICT = dict(CONFIG.reasons)
def make_report_actionrows(
report_id: str, *, askinfo_disabled: bool = False
) -> List[Dict]:
return [
create_actionrow(
create_select(
options=[
create_select_option(title, value=value)
for value, title in CONFIG.reasons
],
placeholder='Global block',
min_values=1,
max_values=1,
custom_id=f'reportaction_{report_id}_block',
)
),
create_actionrow(
create_button(
style=ButtonStyle.green,
label='Ignore',
custom_id=f'reportaction_{report_id}_ignore',
),
create_button(
style=ButtonStyle.blurple,
label='Ask for more info',
custom_id=f'reportaction_{report_id}_askinfo',
disabled=askinfo_disabled,
),
),
]
class Permissions(Enum):
GLOBAL_MOD_ONLY = {
CONFIG.server.id: [
create_permission(
CONFIG.server.roles.global_mod,
SlashCommandPermissionType.ROLE,
True,
),
create_permission(
CONFIG.server.roles.everyone,
SlashCommandPermissionType.ROLE,
False,
),
]
}
DEVELOPER_ONLY = {
CONFIG.server.id: [
create_permission(
CONFIG.server.roles.developer,
SlashCommandPermissionType.ROLE,
True,
),
create_permission(
CONFIG.server.roles.everyone,
SlashCommandPermissionType.ROLE,
False,
),
]
}
async def send_report_embed(
client: Client,
*,
reported: Member,
reporter: Member,
reason: str,
timestamp: datetime,
report_id: str,
message: bool = False,
):
channel = client.get_channel(CONFIG.server.channels.reports)
embed = Embed(
title='New report',
color=Color.gold(),
timestamp=timestamp.replace(tzinfo=timezone.utc),
)
embed.add_field(
name='Reported', value=format_user_info(reported), inline=False
)
embed.add_field(
name='Reporter', value=format_user_info(reporter), inline=False
)
embed.add_field(
name='Reported message' if message else 'Reason',
value=reason,
inline=False,
)
embed.set_footer(text=report_id)
await channel.send(
'@here',
embed=embed,
components=make_report_actionrows(report_id),
)
async def create_block(
client: Client, *, user_id: int, reason: str, moderator_id: int
) -> Block:
block = Block(
user_id=user_id,
reason=reason,
moderator_id=moderator_id,
)
block.save()
user = await client.fetch_user(user_id)
moderator = await client.fetch_user(moderator_id)
embed = Embed(
title='Global block created',
description="Due to a violation of Discord's rules, you've been "
'globally banned from all servers Blockbot is in and reported to '
"Discord's Trust & Safety team.",
color=Color.dark_red(),
)
embed.add_field(name='Reason', value=REASONS_DICT[reason])
embed.add_field(
name='Appeal',
value=f'https://discord.gg/{CONFIG.server.appeals_invite}',
)
embed.set_footer(text=f'User ID: {user_id}')
try:
await user.send(embed=embed)
except Forbidden:
logger.warning(f'Failed to send message to {user}')
channel = client.get_channel(CONFIG.server.channels.block_logs)
embed = Embed(
title='New block',
color=Color.dark_red(),
timestamp=block.timestamp.replace(tzinfo=timezone.utc),
)
embed.add_field(name='User', value=format_user_info(user))
embed.add_field(name='Moderator', value=format_user_info(moderator))
embed.add_field(name='Reason', value=REASONS_DICT[reason])
block_alert = await channel.send(embed=embed)
await block_alert.publish()
for guild in client.guilds:
if guild.id not in CONFIG.noban_servers:
try:
member = await guild.fetch_member(user_id)
await ban_user(client, guild, member)
except NotFound:
continue
async def ban_user(client: Client, guild: Guild, user: Member):
block = Block.objects.get(user_id=user.id) # pylint: disable=no-member
moderator = client.get_user(block.moderator_id)
logger.debug(f'Banning {user} from {guild}')
await guild.ban(
user,
reason=f'Global block by {moderator} ({moderator.id})\n\n'
f'{REASONS_DICT[block.reason]}',
)
embed = Embed(
title=f'Banned from {guild.name}',
description='You were banned from this server due to your global block'
" for violating Discord's rules.",
)
embed.add_field(name='Reason', value=REASONS_DICT[block.reason])
embed.add_field(
name='Appeal',
value=f'https://discord.gg/{CONFIG.server.appeals_invite}',
)
embed.set_footer(text=f'User ID: {user.id}')
try:
await user.send(embed=embed)
except Forbidden:
logger.warning(f'Failed to send message to {user}')
def format_user_info(user: User) -> str:
return f'{user.mention}\n`{user}`\n`{user.id}`'