-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
181 lines (147 loc) · 7.1 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
import os
import datetime
import time
import asyncio
import logging
import traceback
from discord.ext import commands
import discord
import pytz
from core import utils
'''from threading import Thread
import keep_alive
Thread(target=keep_alive.run).start()'''
logging.basicConfig(
format='[%(asctime)s.%(msecs)03d] (%(name)s.%(funcName)s:%(lineno)d) %(levelname)s: %(message)s',
datefmt='%d/%m/%Y %H:%M:%S',
level=logging.INFO
)
log = logging.getLogger(__name__)
logging.getLogger('discord.http').setLevel(logging.WARNING)
logging.getLogger('discord.gateway').setLevel(logging.WARNING)
bot = commands.Bot(
owner_id=585965764952195103,
command_prefix=commands.when_mentioned_or('>'),
case_insensitive=True,
intents=discord.Intents.default().all(),
activity=discord.Activity(type=discord.ActivityType.listening, name='comandos com " > " | >help')
)
bot._BotBase__cogs = commands.core._CaseInsensitiveDict() # pylint: disable=protected-access
@bot.event
async def on_connect():
if not hasattr(bot, 'appinfo'):
bot.appinfo = await bot.application_info()
bot.uptime = time.time()
@bot.event
async def on_disconnect():
elapsed = datetime.datetime.now() - datetime.datetime.fromtimestamp(bot.uptime)
elapsed = str(datetime.timedelta(seconds=elapsed.seconds))
channel = bot.get_channel(856341000843034686)
await channel.send(f'Estou offline! Eu estive online por {elapsed}')
print(f'Estou offline! Eu estive online por {elapsed}')
@bot.event
async def on_ready():
timeanddate = datetime.datetime.now(pytz.timezone('Brazil/East'))
#weekday = ['Segunda-Feira', 'Terça-Feira', 'Quarta-Feira', 'Quinta-Feira', 'Sexta-Feira', 'Sábado', 'Domingo'][timeanddate.weekday()]
weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][timeanddate.weekday()]
print(
'\033[92m' + 'BOT IS NOW ONLINE.' + '\033[0m\n'
f'Bot user: {bot.user} ID: {bot.user.id}. Running Discord.py {discord.__version__}\n'
f'{timeanddate.strftime(f"Date: %d/%m/%y, {weekday}. Hour: %H:%M:%S")}')
@bot.event
async def on_error(event, *args, **kwargs):
embed = discord.Embed(title=':x: Event Error', colour=0xe74c3c)
embed.add_field(name='Event', value=event)
embed.description = f'```py\n{traceback.print_exc()}\n```'
embed.timestamp = datetime.datetime.utcnow()
await bot.appinfo.owner.send(embed=embed)
traceback.print_exc()
@bot.event
async def on_command_error(ctx, error):
if hasattr(ctx.command, 'on_error'): # Ignore commands with local error handlers
return
cog = ctx.cog
if cog:
if not cog._get_overridden_method(cog.cog_command_error) is None: # pylint: disable=protected-access
return
error = getattr(error, 'original', error)
if isinstance(error, commands.MaxConcurrencyReached):
return await ctx.reply('Já tem alguém usando esse comando!')
elif isinstance(error, commands.BotMissingPermissions):
permissions = '\n'.join([f'{permission}' for permission in error.missing_perms])
return await ctx.reply(f'Eu não consegui fazer o que foi pedido pois não tenho permissão para:\n{permissions}')
elif isinstance(error, commands.TooManyArguments):
return await ctx.reply('Argumentos inválidos')
elif isinstance(error, commands.BadArgument):
return await ctx.reply('Argumentos inválidos')
elif isinstance(error, commands.MissingRequiredArgument):
if hasattr(error, 'param'):
return await ctx.reply(f'O argumento do parâmetro `{error.param.name}` está faltando!')
else:
return await ctx.reply('Argumentos inválidos')
elif isinstance(error, commands.DisabledCommand):
return await ctx.reply('Esse comando está desativado')
elif isinstance(error, commands.NoPrivateMessage):
return await ctx.reply('Esse comando não pode ser usado em DMs')
elif isinstance(error, commands.NotOwner):
return
elif isinstance(error, commands.CommandNotFound):
await ctx.message.add_reaction('❔')
check = utils.funcs.reaction_check(ctx.message, '❔', ctx.message.author)
try:
reaction, _ = await bot.wait_for('reaction_add', timeout=5.0, check=check)
await reaction.remove(bot.user)
await ctx.send_help()
# helpcmd = discord.utils.get(bot.commands, name='ajuda') # doesnt work on 1.7.3?
# if helpcmd:
# await helpcmd.callback(ctx)
except asyncio.TimeoutError:
await ctx.message.remove_reaction('❔', bot.user)
return
elif isinstance(error, commands.CommandOnCooldown):
if ctx.author.id == bot.appinfo.owner.id:
ctx.command.reset_cooldown(ctx)
try:
return await ctx.command.reinvoke(ctx)
except Exception as exc: # pylint: disable=broad-except
return await on_command_error(ctx, exc)
return await ctx.reply(f'O comando tá em cooldown! Espera uns {error.retry_after:.1f} segundos aí.')
else:
if isinstance(error, discord.errors.HTTPException):
if hasattr(error, 'status') and hasattr(error, 'code'):
if error.status == 400 and error.code == 50006:
await ctx.reply('Parece que a ordem dos argumentos não está certa ou há argumentos faltando na mensagem.')
else:
await ctx.reply('Houve um erro ao reproduzir esse comando, as informações do erro foram enviadas para o desenvolvedor.')
print(f'Erro na execução do comando: {ctx.command}: {error}')
traceback.print_exception(type(error), error, error.__traceback__)
log.warning('Erro na execução do comando: %s: %s', ctx.command, error)
async def main():
log.info('Loading extensions...')
start_perf = time.perf_counter()
failed = False
additional_ext = ['jishaku']
for ext in additional_ext:
try:
await bot.load_extension(ext)
except (commands.ExtensionNotFound, commands.NoEntryPointError, commands.ExtensionFailed, ModuleNotFoundError) as exc:
log.warning('Failed to load external extension: %s: %s', ext, exc)
failed = True
for root, _, files in os.walk(os.path.dirname(__file__) + r'\core\commands\cogs'):
root = root.replace(os.path.dirname(__file__), '')
while root.startswith('\\'):
root = root[1:]
for file in files:
if file.endswith('.py'):
try:
await bot.load_extension('{}.{}'.format(root.replace('\\\\', '.').replace('\\', '.').replace('/', '.'), file[:-3]))
except (commands.ExtensionNotFound, commands.NoEntryPointError, commands.ExtensionFailed, ModuleNotFoundError) as exc:
log.warning('Failed to load extension: %s: %s', file, exc)
traceback.print_exc()
failed = True
if not failed:
log.info('Loaded all extensions successfully in %fs', time.perf_counter() - start_perf)
async with bot:
await bot.start(os.environ['bot_token'])
if __name__ == '__main__':
asyncio.run(main())