2019-05-30 00:59:20 +02:00
|
|
|
import datetime
|
2019-09-08 23:05:43 +02:00
|
|
|
import logging
|
2019-05-30 00:59:20 +02:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2018-12-03 01:26:23 +01:00
|
|
|
import aiohttp
|
2019-05-30 00:59:20 +02:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
import config
|
2019-09-08 23:05:43 +02:00
|
|
|
from cogs.utils.lang import _
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
description = """
|
|
|
|
Je suis TuxBot, le bot qui vit de l'OpenSource ! ;)
|
|
|
|
"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2019-07-28 20:55:00 +02:00
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
l_extensions = (
|
|
|
|
'cogs.admin',
|
2019-09-08 23:05:43 +02:00
|
|
|
'cogs.basaics',
|
2019-05-30 00:59:20 +02:00
|
|
|
)
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
|
|
|
|
async def _prefix_callable(bot, message):
|
|
|
|
base = [] if config.prefix is None else config.prefix
|
|
|
|
|
|
|
|
# if message.guild is not None:
|
|
|
|
# base.extend(bot.prefixes.get(message.guild.id))
|
|
|
|
return commands.when_mentioned_or(base)
|
2017-06-11 20:04:03 +02:00
|
|
|
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
class TuxBot(commands.AutoShardedBot):
|
|
|
|
__slots__ = ('uptime', 'config', 'session')
|
|
|
|
|
|
|
|
def __init__(self, unload):
|
|
|
|
super().__init__(command_prefix=_prefix_callable,
|
|
|
|
description=description, pm_help=None,
|
|
|
|
help_command=None, help_attrs=dict(hidden=True))
|
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
self.uptime = datetime.datetime.utcnow()
|
|
|
|
self.config = config
|
2019-09-08 23:05:43 +02:00
|
|
|
self.prefixes = {}
|
2019-05-30 00:59:20 +02:00
|
|
|
self.session = aiohttp.ClientSession(loop=self.loop)
|
|
|
|
|
|
|
|
for extension in l_extensions:
|
2019-09-08 23:05:43 +02:00
|
|
|
if extension not in unload:
|
|
|
|
try:
|
|
|
|
self.load_extension(extension)
|
|
|
|
except Exception as e:
|
|
|
|
print(_("Failed to load extension : ") + extension,
|
|
|
|
file=sys.stderr)
|
|
|
|
traceback.print_exc()
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
async def on_command_error(self, ctx, error):
|
|
|
|
if isinstance(error, commands.NoPrivateMessage):
|
2019-09-08 23:05:43 +02:00
|
|
|
await ctx.author.send(
|
|
|
|
_('This command cannot be used in private messages.')
|
|
|
|
)
|
2019-05-30 00:59:20 +02:00
|
|
|
elif isinstance(error, commands.DisabledCommand):
|
2019-09-08 23:05:43 +02:00
|
|
|
await ctx.author.send(
|
|
|
|
_('Sorry. This command is disabled and cannot be used.')
|
|
|
|
)
|
2019-05-30 00:59:20 +02:00
|
|
|
elif isinstance(error, commands.CommandInvokeError):
|
2019-09-08 23:05:43 +02:00
|
|
|
print(_('In ') + f'{ctx.command.qualified_name}:', file=sys.stderr)
|
2019-05-30 00:59:20 +02:00
|
|
|
traceback.print_tb(error.original.__traceback__)
|
|
|
|
print(f'{error.original.__class__.__name__}: {error.original}',
|
|
|
|
file=sys.stderr)
|
2019-09-08 23:05:43 +02:00
|
|
|
elif isinstance(error, commands.ArgumentParsingError):
|
|
|
|
await ctx.send(error)
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
async def on_ready(self):
|
2019-09-08 23:05:43 +02:00
|
|
|
if not hasattr(self, 'uptime'):
|
|
|
|
self.uptime = datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
print(_('Ready:') + f' {self.user} (ID: {self.user.id})')
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
await self.change_presence(status=discord.Status.dnd,
|
|
|
|
activity=discord.Game(
|
2019-09-08 23:05:43 +02:00
|
|
|
name=self.config.activity
|
|
|
|
))
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def on_resumed():
|
|
|
|
print('resumed...')
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
@property
|
|
|
|
def logs_webhook(self):
|
|
|
|
logs_webhook = self.config.logs_webhook
|
|
|
|
webhook = discord.Webhook.partial(id=logs_webhook.get('id'),
|
|
|
|
token=logs_webhook.get('token'),
|
|
|
|
adapter=discord.AsyncWebhookAdapter(
|
|
|
|
self.session))
|
|
|
|
return webhook
|
2019-05-30 00:59:20 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
async def close(self):
|
|
|
|
await super().close()
|
|
|
|
await self.session.close()
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
def run(self):
|
2019-09-08 23:05:43 +02:00
|
|
|
super().run(config.token, reconnect=True)
|