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
|
2019-09-21 23:17:45 +02:00
|
|
|
from collections import deque, Counter
|
2019-05-30 00:59:20 +02:00
|
|
|
|
2018-12-03 01:26:23 +01:00
|
|
|
import aiohttp
|
2019-05-30 00:59:20 +02:00
|
|
|
import discord
|
2019-09-10 23:20:56 +02:00
|
|
|
import git
|
2019-05-30 00:59:20 +02:00
|
|
|
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-09 22:40:17 +02:00
|
|
|
from cogs.utils.config import Config
|
2019-09-12 15:43:57 +02:00
|
|
|
from cogs.utils.lang import Texts
|
2019-09-10 23:20:56 +02:00
|
|
|
from cogs.utils.version import Version
|
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 ! ;)
|
|
|
|
"""
|
2019-09-12 15:43:57 +02:00
|
|
|
build = git.Repo(search_parent_directories=True).head.object.hexsha
|
2019-09-08 23:05:43 +02:00
|
|
|
|
|
|
|
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-15 01:25:32 +02:00
|
|
|
'cogs.basics',
|
2019-09-22 01:55:43 +02:00
|
|
|
'cogs.utility',
|
2019-09-21 00:11:29 +02:00
|
|
|
'cogs.logs',
|
2019-10-06 01:49:30 +02:00
|
|
|
'cogs.poll',
|
2019-09-09 22:40:17 +02:00
|
|
|
'jishaku',
|
2019-05-30 00:59:20 +02:00
|
|
|
)
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def _prefix_callable(bot, message: discord.message) -> list:
|
2019-10-26 23:19:47 +02:00
|
|
|
extras = ['.']
|
2019-09-09 22:40:17 +02:00
|
|
|
if message.guild is not None:
|
|
|
|
extras = bot.prefixes.get(str(message.guild.id), [])
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-09-09 22:40:17 +02:00
|
|
|
return commands.when_mentioned_or(*extras)(bot, message)
|
2017-06-11 20:04:03 +02:00
|
|
|
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
class TuxBot(commands.AutoShardedBot):
|
|
|
|
|
2019-10-06 23:49:00 +02:00
|
|
|
def __init__(self, unload: list, database):
|
2019-09-12 15:43:57 +02:00
|
|
|
super().__init__(command_prefix=_prefix_callable, pm_help=None,
|
|
|
|
help_command=None, description=description,
|
|
|
|
help_attrs=dict(hidden=True),
|
2019-09-21 00:11:29 +02:00
|
|
|
activity=discord.Game(
|
|
|
|
name=Texts().get('Starting...'))
|
|
|
|
)
|
2019-09-08 23:05:43 +02:00
|
|
|
|
2019-09-21 23:17:45 +02:00
|
|
|
self.socket_stats = Counter()
|
|
|
|
self.command_stats = Counter()
|
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
self.uptime: datetime = datetime.datetime.utcnow()
|
2019-05-30 00:59:20 +02:00
|
|
|
self.config = config
|
2019-10-06 23:49:00 +02:00
|
|
|
self.database = database
|
2019-09-09 22:40:17 +02:00
|
|
|
self._prev_events = deque(maxlen=10)
|
2019-05-30 00:59:20 +02:00
|
|
|
self.session = aiohttp.ClientSession(loop=self.loop)
|
|
|
|
|
2019-09-09 22:40:17 +02:00
|
|
|
self.prefixes = Config('prefixes.json')
|
|
|
|
self.blacklist = Config('blacklist.json')
|
|
|
|
|
2019-10-06 23:49:00 +02:00
|
|
|
self.version = Version(10, 0, 0, pre_release='a21', build=build)
|
2019-09-10 23:20:56 +02:00
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
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:
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("Failed to load extension : ")
|
|
|
|
+ extension, file=sys.stderr)
|
|
|
|
log.error(Texts().get("Failed to load extension : ")
|
|
|
|
+ extension, exc_info=e)
|
2019-09-09 22:40:17 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def is_owner(self, user: discord.User) -> bool:
|
|
|
|
return user.id in config.authorized_id
|
|
|
|
|
2019-09-09 22:40:17 +02:00
|
|
|
async def on_socket_response(self, msg):
|
|
|
|
self._prev_events.append(msg)
|
2019-05-30 00:59:20 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def on_command_error(self, ctx: discord.ext.commands.Context, error):
|
2019-05-30 00:59:20 +02:00
|
|
|
if isinstance(error, commands.NoPrivateMessage):
|
2019-09-12 15:43:57 +02:00
|
|
|
await ctx.author.send(
|
|
|
|
Texts().get("This command cannot be used in private messages.")
|
|
|
|
)
|
2019-09-10 18:37:38 +02:00
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
elif isinstance(error, commands.DisabledCommand):
|
2019-09-12 15:43:57 +02:00
|
|
|
await ctx.author.send(
|
2019-09-21 00:11:29 +02:00
|
|
|
Texts().get(
|
|
|
|
"Sorry. This command is disabled and cannot be used."
|
|
|
|
)
|
2019-09-12 15:43:57 +02:00
|
|
|
)
|
2019-09-10 18:37:38 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def process_commands(self, message: discord.message):
|
2019-09-09 22:40:17 +02:00
|
|
|
ctx = await self.get_context(message)
|
|
|
|
|
|
|
|
if ctx.command is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
await self.invoke(ctx)
|
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def on_message(self, message: discord.message):
|
2019-09-12 15:43:57 +02:00
|
|
|
if message.author.bot \
|
|
|
|
or message.author.id in self.blacklist \
|
2019-10-26 23:26:00 +02:00
|
|
|
or (message.guild is not None
|
|
|
|
and message.guild.id in self.blacklist):
|
2019-09-09 22:40:17 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
await self.process_commands(message)
|
|
|
|
|
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()
|
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
print(Texts().get("Ready:") + f' {self.user} (ID: {self.user.id})')
|
2019-09-10 23:20:56 +02:00
|
|
|
print(self.version)
|
2019-05-30 00:59:20 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
presence: dict = dict(status=discord.Status.dnd)
|
|
|
|
if self.config.activity is not None:
|
|
|
|
presence.update(activity=discord.Game(name=self.config.activity))
|
|
|
|
|
|
|
|
await self.change_presence(**presence)
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def on_resumed():
|
|
|
|
print('resumed...')
|
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
@property
|
2019-09-10 18:23:24 +02:00
|
|
|
def logs_webhook(self) -> discord.Webhook:
|
2019-09-08 23:05:43 +02:00
|
|
|
logs_webhook = self.config.logs_webhook
|
2019-10-06 01:49:30 +02:00
|
|
|
webhook = discord.Webhook.partial(
|
|
|
|
id=logs_webhook.get('id'),
|
|
|
|
token=logs_webhook.get('token'),
|
|
|
|
adapter=discord.AsyncWebhookAdapter(
|
|
|
|
self.session
|
|
|
|
)
|
|
|
|
)
|
2019-09-10 18:37:38 +02:00
|
|
|
|
2019-09-08 23:05:43 +02:00
|
|
|
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()
|
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)
|