-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
227 lines (193 loc) · 8.61 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
# import dependencies
import os
from discord.ext import commands
import discord
import datetime
import json, asyncio
import copy
import configparser
import traceback
import sys
import os
import re
import ast
import config
# sets working directory to bot's folder
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
token = config.token
prefix = config.prefix
description = config.description
bot = commands.Bot(command_prefix=prefix, description=description)
bot.dir_path = os.path.dirname(os.path.realpath(__file__))
@bot.check # taken and modified from https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#global-checks
async def globally_block_dms(ctx):
if ctx.guild is None:
raise discord.ext.commands.NoPrivateMessage('test')
return False
return True
# mostly taken from https://github.com/Rapptz/discord.py/blob/async/discord/ext/commands/bot.py
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, discord.ext.commands.errors.CommandNotFound):
pass # ...don't need to know if commands don't exist
elif isinstance(error, discord.ext.commands.errors.CheckFailure):
await ctx.send("You don't have permission to use this command.")
elif isinstance(error, discord.ext.commands.NoPrivateMessage):
await ctx.send("You cannot use this command in DMs!")
elif isinstance(error, discord.ext.commands.errors.MissingRequiredArgument):
formatter = commands.formatter.HelpFormatter()
await ctx.send("You are missing required arguments.\n{}".format(formatter.format_help_for(ctx, ctx.command)[0]))
else:
if ctx.command:
await ctx.send("An error occurred while processing the `{}` command.".format(ctx.command.name))
print('Ignoring exception in command {0.command} in {0.message.channel}'.format(ctx))
tb = traceback.format_exception(type(error), error, error.__traceback__)
error_trace = "".join(tb)
print(error_trace)
if bot.err_logs_channel:
embed = discord.Embed(description=error_trace)
await bot.err_logs_channel.send("An error occurred while processing the `{}` command in channel `{}`.".format(ctx.command.name, ctx.message.channel), embed=embed)
@bot.event
async def on_error(event_method, *args, **kwargs):
if isinstance(args[0], commands.errors.CommandNotFound):
return
print("Ignoring exception in {}".format(event_method))
tb = traceback.format_exc()
error_trace = "".join(tb)
print(error_trace)
if bot.err_logs_channel:
embed = discord.Embed(description=error_trace)
await bot.err_logs_channel.send("An error occurred while processing `{}`.".format(event_method), embed=embed)
@bot.event
async def on_ready():
# this bot should only ever be in one server anyway
for guild in bot.guilds:
try:
bot.guild = guild
try:
with open("restart.txt") as f:
channel = bot.get_channel(int(f.readline()))
f.close()
await channel.send("Restarted!")
os.remove("restart.txt")
except:
pass
bot.creator = discord.utils.get(guild.members, id=177939404243992578)
bot.log_channel = discord.utils.get(guild.channels, id=config.log_channel)
bot.public_logs = discord.utils.get(guild.channels, id=config.public_logs)
bot.private_logs = discord.utils.get(guild.channels, id=config.private_logs)
bot.message_log_channel = discord.utils.get(guild.channels, id=config.message_log_channel)
bot.err_logs_channel = discord.utils.get(guild.channels, id=config.err_logs_channel)
bot.ignored_channels = {bot.message_log_channel, bot.log_channel}
for id in config.ignored_chans:
bot.ignored_channels.add(discord.utils.get(guild.channels, id=id))
bot.approval = config.approval_bool
if bot.approval:
bot.approval_channel = discord.utils.get(guild.channels, id=config.approval_channel)
bot.default_role = discord.utils.get(guild.roles, name=config.default_role)
if config.nsfw_role != "":
bot.nsfw_role = discord.utils.get(guild.roles, name=config.nsfw_role)
if config.news_role != "":
bot.news_role = discord.utils.get(guild.roles, name=config.news_role)
if config.restrict_role != "":
bot.restrict_role = discord.utils.get(guild.roles, name=config.restrict_role)
bot.staff_roles = []
for role in config.staff_roles:
bot.staff_roles.append(discord.utils.get(guild.roles, name=role))
#colors
bot.green_role = discord.utils.get(guild.roles, name="Green")
bot.blocked_users = set()
for id in config.blocked_users:
bot.blocked_users.add(discord.utils.get(guild.members, id=id))
bot.message_purge = False
if guild.me.nick != None:
old_nick = guild.me.nick
await bot.user.edit(nick=None)
await bot.log_channel.send("Someone changed my name to {} while I was offline 😡".format(old_nick))
print("Initialized on {}.".format(guild.name))
except Exception as e:
print("Failed to initialize on {}".format(guild.name))
print("\t{}".format(e))
# loads extensions
addons = [
'addons.info',
'addons.events',
'addons.utility',
'addons.mod',
'addons.colors'
]
failed_addons = []
for extension in addons:
try:
bot.load_extension(extension)
except Exception as e:
print('{} failed to load.\n{}: {}'.format(extension, type(e).__name__, e))
failed_addons.append([extension, type(e).__name__, e])
if not failed_addons:
print('All addons loaded!')
@bot.check
def check_author_is_blocked(ctx):
return ctx.author not in bot.blocked_users
@bot.command()
async def reload(ctx):
"""Reloads an addon."""
if ctx.author != ctx.guild.owner and ctx.author != bot.creator:
return await ctx.send("You can't use this!")
errors = ""
for addon in os.listdir("addons"):
if ".py" in addon:
addon = addon.replace('.py', '')
try:
bot.unload_extension("addons.{}".format(addon))
bot.load_extension("addons.{}".format(addon))
except Exception as e:
errors += 'Failed to load addon: `{}.py` due to `{}: {}`\n'.format(addon, type(e).__name__, e)
if not errors:
await ctx.send(':white_check_mark: Extensions reloaded.')
else:
await ctx.send(errors)
@bot.command(hidden=True)
async def load(ctx, *, module):
"""Loads an addon"""
if ctx.author != ctx.guild.owner and ctx.author != bot.creator:
return await ctx.send("You can't use this!")
try:
bot.load_extension("addons.{}".format(module))
except Exception as e:
await ctx.send(':anger: Failed!\n```\n{}: {}\n```'.format(type(e).__name__, e))
else:
await ctx.send(':white_check_mark: Extension loaded.')
@bot.command(hidden=True)
async def botedit(ctx, name=""):
"""Edits the bot profile. Takes name only, at the moment. Bot owner only"""
await ctx.message.delete()
if ctx.author != bot.creator:
return
if not name:
name = bot.user.name
return await bot.user.edit(username=name)
async def pingfunc(ctx): # taken from https://github.com/appu1232/Discord-Selfbot/blob/873a2500d2c518e0d25ca5a6f67828de60fbda99/cogs/misc.py#L626
msgtime = ctx.message.created_at.now()
await (await bot.ws.ping())
now = datetime.datetime.now()
return now - msgtime
@bot.command(hidden=True)
async def ping(ctx):
"""Get response time."""
ping = await pingfunc(ctx)
await ctx.send('Response Time: %s ms' % str(ping.microseconds / 1000.0))
@bot.command(hidden=True)
async def avgping(ctx, count=5):
if count > 100:
return await ctx.send("That's too many samples! Limit is 100")
elif count <= 0:
return await ctx.send("You can't take the average of a non positive number of samples!")
ping_total = datetime.timedelta()
for x in range(count):
ping_total += await pingfunc(ctx)
average_ping = ping_total / count
await ctx.send('Average Response Time: %s ms' % str(average_ping.microseconds / 1000.0))
# Execute
print('Bot directory: ', dir_path)
bot.run(token)