-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·392 lines (337 loc) · 19.4 KB
/
main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!venv/bin/python
import traceback
from discord.ext import commands
import discord
from discord import utils
import datetime
import time
import json
import asyncio
from discord.ext.commands import DefaultHelpCommand
from cogs.checks import owner_check, in_bot_channel
def join_attachment_urls(attachments: [discord.Attachment]):
return '\n'.join(map(lambda a: a.url, attachments))
class ACoolBot(commands.Bot):
def __init__(self, **options):
super().__init__(self.command_prefix, **options)
self.member_converter = commands.MemberConverter()
self.all_cogs = [
'cogs.Fun',
'cogs.Moderation',
'cogs.Reddit',
'cogs.Abuse'
]
self.pages = {}
self.data = json.load(open('data.json'))
self.add_command(self.reload_cogs)
self.add_command(self.invite)
self.add_command(self.give_role)
self.add_command(self.leave)
self.help_command.add_check(in_bot_channel)
def command_prefix(self, bot, message: discord.Message):
if not message.guild:
return '*'
prefixes = self.get_data(message.guild.id, 'prefix', ['*'])
return commands.when_mentioned_or(*prefixes)(self, message)
def set_data(self, gid: int, name: str, val):
try:
self.data[str(gid)].update({name: val})
except KeyError:
self.data.update({str(gid): {name: val}})
json.dump(self.data, open('data.json', 'w'))
def get_data(self, gid: int, name, default=None):
try:
return self.data[str(gid)][name]
except KeyError:
self.set_data(gid, name, default)
return self.data[str(gid)][name]
async def on_ready(self):
for extension in self.all_cogs:
await self.load_extension(extension)
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.watching,
name="humanity withering away"))
@staticmethod
async def give_role_voice_channel(member: discord.Member, before: discord.VoiceState,
after: discord.VoiceState):
if before.channel == after.channel:
return
if after.channel is not None:
if before.channel is not None:
before_role = utils.find(lambda r: r.name.lower() == before.channel.name.lower(), member.guild.roles)
after_role = utils.find(lambda r: r.name.lower() == after.channel.name.lower(), member.guild.roles)
if after_role is None:
after_role = utils.find(lambda r: r.name.lower() == after.channel.name.lower(),
after.channel.guild.roles)
if after_role is None:
after_role = await after.channel.guild.create_role(name=after.channel.name)
roles = member.roles
if before.channel is not None:
before_role = utils.find(lambda r: r.name == before.channel.name, roles)
if before_role in roles:
await member.remove_roles(before_role, reason='voice connectivity')
if after.channel is not None:
if after_role not in roles:
await member.add_roles(after_role, reason='Voice connectivity')
async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState,
after: discord.VoiceState):
await self.give_role_voice_channel(member, before, after)
# await self.create_channel_type(member, before, after)
async def create_channel_type(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
if before.channel is not None:
if len(before.channel.members) == 0:
type = before.channel.name.strip(' I')
voice_channels = list(filter(lambda c: type == c.name.strip(' I') and len(c.members) == 0,
member.guild.channels.copy()))
text_channel = list(filter(lambda c: type.lower().replace(' ', "-") == c.name.strip(' i'),
member.guild.channels.copy()))
if len(voice_channels) > 1:
await voice_channels[0].delete()
if len(text_channel) > 1:
await text_channel[0].delete()
role = discord.utils.find(lambda r: r.name == voice_channels[-1].name, member.guild.roles.copy())
if role is not None:
await role.delete()
if after.channel is not None:
type = after.channel.name.strip(' I')
voice_channels = list(filter(lambda c: type == c.name.strip(' I') and len(c.members) == 0,
member.guild.channels.copy()))
text_channel = list(filter(lambda c: type.lower().replace(' ', "-") == c.name.strip(' i'),
member.guild.channels.copy()))
if len(voice_channels) == 0:
guild = member.guild
name = type + ' ' + 'I' * (len(list(filter(lambda c: type == c.name.strip(' I'),
member.guild.channels.copy()))) + 1)
try:
def_role_name = self.get_data(guild.id, 'default role')
except KeyError:
self.set_data(guild.id, 'default role', 'Member')
def_role_name = self.get_data(guild.id, 'default role')
def_role = utils.find(lambda r: r.name == def_role_name, guild.roles.copy())
if def_role is None:
def_role = await guild.create_role(name=def_role_name)
role = await guild.create_role(name=name, color=discord.Color(int('303136', 16)))
overwrites_text = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
role: discord.PermissionOverwrite(read_messages=True)
}
overwrites_voice = {
guild.default_role: discord.PermissionOverwrite(connect=False),
def_role: discord.PermissionOverwrite(connect=True)
}
await guild.create_voice_channel(name=name, category=after.channel.category,
overwrites=overwrites_voice)
await guild.create_text_channel(name=name.lower().replace(' ', '-'), category=text_channel[0].category,
overwrites=overwrites_text)
@staticmethod
async def on_guild_channel_update(before, after):
if type(before) is discord.VoiceChannel:
if before.name is not after.name:
role = discord.utils.find(lambda r: r.name == before.name, before.guild.roles)
await role.edit(name=after.name)
@staticmethod
async def on_guild_channel_delete(before):
if type(before) is discord.VoiceChannel:
role = discord.utils.find(lambda r: r.name == before.name, before.guild.roles)
await role.delete()
async def on_command_error(self, ctx: commands.Context, error):
ignored = commands.CommandNotFound
error = getattr(error, 'original', error)
if isinstance(error, ignored):
return
if hasattr(ctx.command, 'on_error'):
return
elif isinstance(error, commands.DisabledCommand):
return
elif isinstance(error, commands.CheckFailure):
return
elif isinstance(error, commands.NoPrivateMessage):
try:
return await ctx.author.send('{} can not be used in Private Messages.'.format(ctx.command))
except:
pass
elif isinstance(error, commands.errors.CommandOnCooldown):
return await ctx.send(error)
elif isinstance(error, commands.UserInputError):
return await ctx.send('Invalid input')
try:
embed = discord.Embed(title='**__Error on command "' + ctx.command.name + '":__**',
description=str(error)[-1024:],
timestamp=datetime.datetime.fromtimestamp(time.time()),
color=990000, type="rich")
except IndexError:
embed = discord.Embed(title='**__Error on command "' + ctx.command.name + '":__**',
description=str(error)[:1024], timestamp=datetime.datetime.fromtimestamp(time.time()),
color=990000, type="rich")
try:
embed.add_field(name="**Type**", value=str(type(error))[-1024:], inline=False)
except IndexError:
embed.add_field(name="**Type**", value=str(type(error))[:1024], inline=False)
try:
embed.add_field(name='**Author**', value=ctx.message.author.nick, inline=False)
except AttributeError:
embed.add_field(name='**Author**', value=ctx.message.author.name, inline=False)
embed.add_field(name="**Full command:**", value=ctx.message.content, inline=False)
if not isinstance(ctx.message.channel, discord.DMChannel):
embed.add_field(name='**Channel**', value='<#' + str(ctx.message.channel.id) + '> `[' +
ctx.message.channel.name + ']`', inline=False)
if ctx.guild:
embed.add_field(name='**Guild**', value=ctx.guild.name, inline=False)
else:
embed.add_field(name='**Guild**', value='Private Message', inline=False)
trace = traceback.format_exception(type(error), error, error.__traceback__, chain=False)
try:
embed.add_field(name="**Traceback:**", value=''.join(trace)[-1024:], inline=False)
except IndexError:
embed.add_field(name="**Traceback:**", value=''.join(trace)[:1024], inline=False)
members = self.get_all_members()
member = discord.utils.find(lambda m: m.id == 254671305268264960, members)
return await member.send(content='', embed=embed)
async def on_message_delete(self, message: discord.Message):
if message.guild:
if message.channel.id not in self.get_data(message.guild.id, "unlogged channels", []):
if self.get_data(message.guild.id, "delete channel"):
channel = discord.utils.find(lambda c: c.id == self.get_data(message.guild.id, "delete channel"),
message.guild.channels)
description = '**User:** {} `[{}]`\n**Channel:** {} `[{}]`\n{}'.format(message.author.mention,
message.author.name,
message.channel.mention,
message.channel.name,
message.content if message.content else '[message data is missing or nonexistent]')
embed = discord.Embed(title='Deleted Message', color=int('0xFF0000', 16), description=description,
type='rich')
if message.attachments:
embed.add_field(name='**Attachments:** ', value='\n'.join((a.proxy_url for a in message.attachments)))
embed.set_footer(text='Message ID: ' + str(message.id))
embed.timestamp = message.created_at
await channel.send(embed=embed)
async def on_message_edit(self, before: discord.Message, after: discord.Message):
if before.guild:
if before.channel.id not in self.get_data(before.guild.id, "unlogged channels", []):
if self.get_data(before.guild.id, "edit channel"):
if before.content == after.content and join_attachment_urls(before.attachments) == join_attachment_urls(after.attachments):
return
channel = discord.utils.find(lambda c: c.id == self.get_data(before.guild.id, "edit channel"),
before.guild.channels)
description = '**User:** {} `[{}]`\n**Channel:** {} `[{}]`'.format(before.author.mention,
before.author.name,
before.channel.mention,
before.channel.name)
embed = discord.Embed(title='Edited Message', color=int('0x00FF00', 16), description=description,
type='rich')
if before.content:
embed.add_field(name='**before**', value=before.content, inline=False)
if after.content:
embed.add_field(name='**after**', value=after.content, inline=False)
if before.attachments:
embed.add_field(name='**Before Attachments:** ', value=join_attachment_urls(before.attachments), inline=False)
if after.attachments:
embed.add_field(name='**After Attachments:** ', value=join_attachment_urls(after.attachments), inline=False)
embed.set_footer(text='Message ID: ' + str(after.id))
if after.edited_at:
embed.timestamp = after.edited_at
await channel.send(embed=embed)
@staticmethod
@commands.command(name='reload-cogs', aliases=['rc', 'reload'])
@owner_check()
async def reload_cogs(ctx, intention):
for cog in ctx.bot.all_cogs:
try:
await ctx.bot.unload_extension(cog)
except commands.ExtensionNotLoaded:
pass
await ctx.bot.load_extension(cog)
await ctx.send('Done!')
async def on_message(self, message: discord.Message):
if not message.author == self.user:
await self.process_commands(message)
if message.guild:
admin_only_channels = self.get_data(message.guild.id, "admin only channels", [])
watchlist_log = self.get_data(message.guild.id, 'watchlist-log', None)
watchlist = [w[0] for w in self.get_data(message.guild.id, 'watchlist', [])]
watchlist_log = discord.utils.find(lambda c: c.id == watchlist_log, message.guild.text_channels)
if watchlist_log:
if message.author.id in watchlist and not message.author.bot:
description = '**User:** {} `[{}]`\n**Channel:** {} `[{}]`\n{}'.format(message.author.mention,
message.author.name,
message.channel.mention,
message.channel.name,
message.content)
embed = discord.Embed(title='Watchlist Message', color=int('0xFF00FF', 16), description=description,
type='rich')
if message.attachments:
embed.add_field(name='**Attachments:** ', value='\n'.join([a.proxy_url
for a in message.attachments]))
embed.set_footer(text='Message ID: ' + str(message.id))
embed.timestamp = message.created_at
await watchlist_log.send(embed=embed)
if admin_only_channels:
if message.channel.id in admin_only_channels and not self.is_admin(message.author):
await message.delete()
@staticmethod
@commands.command(name='invite')
@owner_check()
async def invite(ctx: commands.Context, intention):
await ctx.send(
'https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions=8'.format(ctx.bot.user.id))
@staticmethod
@commands.command(name='leave')
@owner_check()
async def leave(ctx: commands.Context, intention):
await ctx.guild.leave()
@staticmethod
def is_admin(author: discord.Member):
return author.guild_permissions.administrator and not author.bot
async def embeds_scroller(self, ctx: commands.Context, embeds):
embeds[0].set_footer(text='page: 1/{}'.format(len(embeds)))
message = await ctx.send(embed=embeds[0])
await message.add_reaction('◀')
await asyncio.sleep(0.1)
await message.add_reaction('▶')
self.pages.update({message.id: 0})
def scroll(reaction: discord.Reaction, user: discord.User):
if reaction.message.id == message.id and not user.bot:
if user == ctx.author and str(reaction.emoji) == '◀':
self.pages.update({reaction.message.id: self.pages[reaction.message.id] - 1})
elif user == ctx.author and str(reaction.emoji) == '▶':
self.pages.update({reaction.message.id: self.pages[reaction.message.id] + 1})
return True
else:
return False
while True:
try:
reaction, user = await self.wait_for('reaction_add', check=scroll, timeout=120.0)
if user is not self:
await message.remove_reaction(reaction, user)
if user == ctx.author:
index = self.pages[message.id] % len(embeds)
embeds[index].set_footer(text='page: {page}/{total}'.format(page=self.pages[message.id]
% len(embeds) + 1,
total=len(embeds)))
await message.edit(embed=embeds[index])
except asyncio.TimeoutError:
await message.remove_reaction('▶', self.user)
await message.remove_reaction('◀', self.user)
break
return
def is_banned_word(self, message: discord.Message):
"""
checks whether a banned word is in a message
:param message: discord message object
return bool: True if banned word is found else False
"""
banned_words_list = self.get_data(message.guild.id, 'banned words', [])
for banned_word in banned_words_list:
if banned_word in message.content:
return True
return False
@staticmethod
@commands.command('pyramid', hidden=True)
async def give_role(ctx, intention):
if ctx.guild and ctx.guild.id == 530154962777407509:
rules_role = ctx.guild.get_role(542308860937895949)
await ctx.author.add_roles(rules_role, reason='give_role command')
if __name__ == '__main__':
intents = discord.Intents.all()
bot = ACoolBot(intents=intents, max_messages=10000, owner_id=254671305268264960)
key = json.load(open('DiscordKey.json'))
bot.run(key["key"])