forked from snightshade/TuxedoLegacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
129 lines (111 loc) · 4.8 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
import traceback
import json
import discord
from discord.ext import commands
from discord.ext.commands import errors as commands_errors
from discord import utils as dutils
import random
import asyncio
import raven
import rethinkdb as r
import sys
from utils import permissions
nopls = [110373943822540800]
class Bot(commands.Bot):
def __init__(self, **options):
super().__init__(self.getPrefix, **options)
print('Performing pre-run tasks...')
self.cmd_help = cmd_help
self.maintenance = False
with open("config.json") as f:
self.config = json.load(f)
self.prefix = self.config.get('BOT_PREFIX')
self.version = self.config.get('VERSION')
self.remove_command("help")
self.init_raven()
self.init_rethinkdb()
print('Pre-run tasks complete.')
async def getPrefix(self, bot, msg):
return commands.when_mentioned_or(*self.prefix)(bot, msg)
async def on_ready(self):
app_info = await self.application_info()
self.invite_url = dutils.oauth_url(app_info.id)
print(
f'Logged in as {self.user.name}\nBot invite link: {self.invite_url}')
await self.change_presence(game=discord.Game(name=f'{self.prefix[0]}help | Version {self.version}', type=0))
self.load_extension('extensions.core')
async def on_message(self, message):
if message.author.bot:
return
if message.author.id in self.config.get('BLOCKED'):
return
if message.content.startswith('pls') and message.guild.id in nopls:
return
if not permissions.owner_id_check(self, str(message.author.id)) and self.maintenance:
return
await self.process_commands(message)
def init_raven(self):
print('Now initialising Sentry...')
self.sentry = raven.Client(self.config['SENTRY'])
print('Sentry initialised.')
def init_rethinkdb(self):
print('Now initialising RethinkDB...')
dbc = self.config['RETHINKDB']
try:
self.conn = r.connect(host=dbc['HOST'], port=dbc['PORT'],
db=dbc['DB'], user=dbc['USERNAME'], password=dbc['PASSWORD'])
except Exception as e:
print('RethinkDB init error!\n{}: {}'.format(type(e).__name__, e))
sys.exit(1)
print('RethinkDB initialisation successful.')
def find_command(self, cmdname:str):
for i in self.commands:
if i.name == cmdname:
return i
return False
async def cmd_help(ctx):
if ctx.invoked_subcommand:
_help = await ctx.bot.formatter.format_help_for(ctx,
ctx.invoked_subcommand)
else:
_help = await ctx.bot.formatter.format_help_for(ctx, ctx.command)
for page in _help:
await ctx.send(page)
bot = Bot()
@bot.listen("on_command_error")
async def on_command_error(ctx, exception):
if isinstance(exception, commands_errors.MissingRequiredArgument):
await cmd_help(ctx)
elif isinstance(exception, commands_errors.CommandInvokeError):
exception = exception.original
_traceback = traceback.format_tb(exception.__traceback__)
_traceback = ''.join(_traceback)
error = discord.Embed(
title="An error has occurred.",
color=0xFF0000,
description="This is (probably) a bug. This has been automatically reported, but you may wanna give ry00001#3487 a poke."
)
sentry_string = "{} in command {}\nTraceback (most recent call last):\n{}{}: {}".format(type(
exception).__name__, ctx.command.qualified_name, _traceback, type(exception).__name__, exception)
print(sentry_string)
error.add_field(name="`{}` in command `{}`".format(type(exception).__name__, ctx.command.qualified_name),
value="```py\nTraceback (most recent call last):\n{}{}: {}```".format(_traceback, type(exception).__name__, exception))
ctx.bot.sentry.captureMessage(sentry_string)
await ctx.send(embed=error)
elif isinstance(exception, commands_errors.CommandOnCooldown):
await ctx.send('This command is on cooldown. You can use this command in `{0:.2f}` seconds.'.format(exception.retry_after))
else:
ctx.send(exception)
@bot.command(aliases=['man'])
async def help(ctx, command: str = None):
if ctx.prefix == "pls " and ctx.invoked_with == "help":
return
cmd = ctx.bot.find_command(command)
helptext = await ctx.bot.formatter.format_help_for(ctx, cmd if cmd is not False else ctx.bot)
helptext = helptext[0]
try:
await ctx.author.send(helptext)
await ctx.send(":mailbox_with_mail: Check your DMs.")
except discord.Forbidden:
await ctx.send(helptext)
bot.run(bot.config["BOT_TOKEN"])