forked from Rapptz/RoboDanny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
377 lines (318 loc) · 13.2 KB
/
bot.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from discord.ext import commands
import discord
from cogs.utils import checks, context, db
from cogs.utils.config import Config
import datetime, re
import json, asyncio
import copy
import logging
import traceback
import aiohttp
import sys
from collections import Counter, deque, defaultdict
import config
import asyncpg
description = """
Hello! I am a bot written by Danny to provide some nice utilities.
"""
log = logging.getLogger(__name__)
initial_extensions = (
'cogs.meta',
'cogs.splatoon',
'cogs.rng',
'cogs.mod',
'cogs.profile',
'cogs.tags',
'cogs.lounge',
'cogs.carbonitex',
'cogs.api',
'cogs.stars',
'cogs.admin',
'cogs.buttons',
'cogs.reminder',
'cogs.stats',
'cogs.emoji',
'cogs.config',
'cogs.dpy',
'cogs.funhouse',
)
def _prefix_callable(bot, msg):
user_id = bot.user.id
base = [f'<@!{user_id}> ', f'<@{user_id}> ']
if msg.guild is None:
base.append('!')
base.append('?')
else:
base.extend(bot.prefixes.get(msg.guild.id, ['?', '!']))
return base
class RoboDanny(commands.AutoShardedBot):
def __init__(self):
allowed_mentions = discord.AllowedMentions(roles=False, everyone=False, users=True)
intents = discord.Intents(
guilds=True,
members=True,
bans=True,
emojis=True,
voice_states=True,
messages=True,
reactions=True,
)
super().__init__(command_prefix=_prefix_callable, description=description,
pm_help=None, help_attrs=dict(hidden=True),
fetch_offline_members=False, heartbeat_timeout=150.0,
allowed_mentions=allowed_mentions, intents=intents)
self.client_id = config.client_id
self.carbon_key = config.carbon_key
self.bots_key = config.bots_key
self.challonge_api_key = config.challonge_api_key
self.session = aiohttp.ClientSession(loop=self.loop)
self._prev_events = deque(maxlen=10)
# shard_id: List[datetime.datetime]
# shows the last attempted IDENTIFYs and RESUMEs
self.resumes = defaultdict(list)
self.identifies = defaultdict(list)
# guild_id: list
self.prefixes = Config('prefixes.json')
# guild_id and user_id mapped to True
# these are users and guilds globally blacklisted
# from using the bot
self.blacklist = Config('blacklist.json')
# in case of even further spam, add a cooldown mapping
# for people who excessively spam commands
self.spam_control = commands.CooldownMapping.from_cooldown(10, 12.0, commands.BucketType.user)
# A counter to auto-ban frequent spammers
# Triggering the rate limit 5 times in a row will auto-ban the user from the bot.
self._auto_spam_count = Counter()
for extension in initial_extensions:
try:
self.load_extension(extension)
except Exception as e:
print(f'Failed to load extension {extension}.', file=sys.stderr)
traceback.print_exc()
def _clear_gateway_data(self):
one_week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
for shard_id, dates in self.identifies.items():
to_remove = [index for index, dt in enumerate(dates) if dt < one_week_ago]
for index in reversed(to_remove):
del dates[index]
for shard_id, dates in self.resumes.items():
to_remove = [index for index, dt in enumerate(dates) if dt < one_week_ago]
for index in reversed(to_remove):
del dates[index]
async def on_socket_response(self, msg):
self._prev_events.append(msg)
async def before_identify_hook(self, shard_id, *, initial):
self._clear_gateway_data()
self.identifies[shard_id].append(datetime.datetime.utcnow())
await super().before_identify_hook(shard_id, initial=initial)
async def on_command_error(self, ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.author.send('This command cannot be used in private messages.')
elif isinstance(error, commands.DisabledCommand):
await ctx.author.send('Sorry. This command is disabled and cannot be used.')
elif isinstance(error, commands.CommandInvokeError):
original = error.original
if not isinstance(original, discord.HTTPException):
print(f'In {ctx.command.qualified_name}:', file=sys.stderr)
traceback.print_tb(original.__traceback__)
print(f'{original.__class__.__name__}: {original}', file=sys.stderr)
elif isinstance(error, commands.ArgumentParsingError):
await ctx.send(error)
def get_guild_prefixes(self, guild, *, local_inject=_prefix_callable):
proxy_msg = discord.Object(id=0)
proxy_msg.guild = guild
return local_inject(self, proxy_msg)
def get_raw_guild_prefixes(self, guild_id):
return self.prefixes.get(guild_id, ['?', '!'])
async def set_guild_prefixes(self, guild, prefixes):
if len(prefixes) == 0:
await self.prefixes.put(guild.id, [])
elif len(prefixes) > 10:
raise RuntimeError('Cannot have more than 10 custom prefixes.')
else:
await self.prefixes.put(guild.id, sorted(set(prefixes), reverse=True))
async def add_to_blacklist(self, object_id):
await self.blacklist.put(object_id, True)
async def remove_from_blacklist(self, object_id):
try:
await self.blacklist.remove(object_id)
except KeyError:
pass
async def query_member_named(self, guild, argument, *, cache=False):
"""Queries a member by their name, name + discrim, or nickname.
Parameters
------------
guild: Guild
The guild to query the member in.
argument: str
The name, nickname, or name + discrim combo to check.
cache: bool
Whether to cache the results of the query.
Returns
---------
Optional[Member]
The member matching the query or None if not found.
"""
if len(argument) > 5 and argument[-5] == '#':
username, _, discriminator = argument.rpartition('#')
members = await guild.query_members(username, limit=100, cache=cache)
return discord.utils.get(members, name=username, discriminator=discriminator)
else:
members = await guild.query_members(argument, limit=100, cache=cache)
return discord.utils.find(lambda m: m.name == argument or m.nick == argument, members)
async def get_or_fetch_member(self, guild, member_id):
"""Looks up a member in cache or fetches if not found.
Parameters
-----------
guild: Guild
The guild to look in.
member_id: int
The member ID to search for.
Returns
---------
Optional[Member]
The member or None if not found.
"""
member = guild.get_member(member_id)
if member is not None:
return member
shard = self.get_shard(guild.shard_id)
if shard.is_ws_ratelimited():
try:
member = await guild.fetch_member(member_id)
except discord.HTTPException:
return None
else:
return member
members = await guild.query_members(limit=1, user_ids=[member_id], cache=True)
if not members:
return None
return members[0]
async def resolve_member_ids(self, guild, member_ids):
"""Bulk resolves member IDs to member instances, if possible.
Members that can't be resolved are discarded from the list.
This is done lazily using an asynchronous iterator.
Note that the order of the resolved members is not the same as the input.
Parameters
-----------
guild: Guild
The guild to resolve from.
member_ids: Iterable[int]
An iterable of member IDs.
Yields
--------
Member
The resolved members.
"""
needs_resolution = []
for member_id in member_ids:
member = guild.get_member(member_id)
if member is not None:
yield member
else:
needs_resolution.append(member_id)
total_need_resolution = len(needs_resolution)
if total_need_resolution == 1:
shard = self.get_shard(guild.shard_id)
if shard.is_ws_ratelimited():
try:
member = await guild.fetch_member(needs_resolution[0])
except discord.HTTPException:
pass
else:
yield member
else:
members = await guild.query_members(limit=1, user_ids=needs_resolution, cache=True)
if members:
yield members[0]
elif total_need_resolution <= 100:
# Only a single resolution call needed here
resolved = await guild.query_members(limit=100, user_ids=needs_resolution, cache=True)
for member in resolved:
yield member
else:
# We need to chunk these in bits of 100...
for index in range(0, total_need_resolution, 100):
to_resolve = needs_resolution[index:index + 100]
members = await guild.query_members(limit=100, user_ids=to_resolve, cache=True)
for member in resolved:
yield member
async def on_ready(self):
if not hasattr(self, 'uptime'):
self.uptime = datetime.datetime.utcnow()
print(f'Ready: {self.user} (ID: {self.user.id})')
async def on_shard_resumed(self, shard_id):
print(f'Shard ID {shard_id} has resumed...')
self.resumes[shard_id].append(datetime.datetime.utcnow())
@discord.utils.cached_property
def stats_webhook(self):
wh_id, wh_token = self.config.stat_webhook
hook = discord.Webhook.partial(id=wh_id, token=wh_token, adapter=discord.AsyncWebhookAdapter(self.session))
return hook
def log_spammer(self, ctx, message, retry_after, *, autoblock=False):
guild_name = getattr(ctx.guild, 'name', 'No Guild (DMs)')
guild_id = getattr(ctx.guild, 'id', None)
fmt = 'User %s (ID %s) in guild %r (ID %s) spamming, retry_after: %.2fs'
log.warning(fmt, message.author, message.author.id, guild_name, guild_id, retry_after)
if not autoblock:
return
wh = self.stats_webhook
embed = discord.Embed(title='Auto-blocked Member', colour=0xDDA453)
embed.add_field(name='Member', value=f'{message.author} (ID: {message.author.id})', inline=False)
embed.add_field(name='Guild Info', value=f'{guild_name} (ID: {guild_id})', inline=False)
embed.add_field(name='Channel Info', value=f'{message.channel} (ID: {message.channel.id}', inline=False)
embed.timestamp = datetime.datetime.utcnow()
return wh.send(embed=embed)
async def process_commands(self, message):
ctx = await self.get_context(message, cls=context.Context)
if ctx.command is None:
return
if ctx.author.id in self.blacklist:
return
if ctx.guild is not None and ctx.guild.id in self.blacklist:
return
bucket = self.spam_control.get_bucket(message)
current = message.created_at.replace(tzinfo=datetime.timezone.utc).timestamp()
retry_after = bucket.update_rate_limit(current)
author_id = message.author.id
if retry_after and author_id != self.owner_id:
self._auto_spam_count[author_id] += 1
if self._auto_spam_count[author_id] >= 5:
await self.add_to_blacklist(author_id)
del self._auto_spam_count[author_id]
await self.log_spammer(ctx, message, retry_after, autoblock=True)
else:
self.log_spammer(ctx, message, retry_after)
return
else:
self._auto_spam_count.pop(author_id, None)
try:
await self.invoke(ctx)
finally:
# Just in case we have any outstanding DB connections
await ctx.release()
async def on_message(self, message):
if message.author.bot:
return
await self.process_commands(message)
async def on_guild_join(self, guild):
if guild.id in self.blacklist:
await guild.leave()
async def close(self):
await super().close()
await self.session.close()
def run(self):
try:
super().run(config.token, reconnect=True)
finally:
with open('prev_events.log', 'w', encoding='utf-8') as fp:
for data in self._prev_events:
try:
x = json.dumps(data, ensure_ascii=True, indent=4)
except:
fp.write(f'{data}\n')
else:
fp.write(f'{x}\n')
@property
def config(self):
return __import__('config')