2019-12-30 00:48:11 +01:00
|
|
|
import contextlib
|
2019-05-30 00:59:20 +02:00
|
|
|
import datetime
|
2020-02-04 18:47:11 +01:00
|
|
|
import json
|
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-12-16 18:12:10 +01:00
|
|
|
from typing import List
|
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
|
2020-02-04 18:47:11 +01:00
|
|
|
import sqlalchemy
|
2019-05-30 00:59:20 +02:00
|
|
|
from discord.ext import commands
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2020-01-15 22:56:54 +01:00
|
|
|
from utils.functions import Config
|
|
|
|
from utils.functions import Texts
|
|
|
|
from utils.functions import Version
|
|
|
|
from utils.functions import ContextPlus
|
2017-06-11 20:04:03 +02:00
|
|
|
|
2020-02-04 18:47:11 +01:00
|
|
|
from utils.models import metadata, database
|
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 ! ;)
|
|
|
|
"""
|
2020-01-01 23:44:46 +01:00
|
|
|
|
2019-09-12 15:43:57 +02:00
|
|
|
build = git.Repo(search_parent_directories=True).head.object.hexsha
|
2020-01-15 01:23:53 +01:00
|
|
|
version = (2, 0, 0)
|
2019-09-08 23:05:43 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2019-07-28 20:55:00 +02:00
|
|
|
|
2019-12-16 18:12:10 +01:00
|
|
|
l_extensions: List[str] = [
|
2020-01-04 02:46:12 +01:00
|
|
|
'cogs.Admin',
|
2020-02-04 18:47:11 +01:00
|
|
|
'cogs.API',
|
2020-01-04 02:46:12 +01:00
|
|
|
'cogs.Help',
|
|
|
|
'cogs.Logs',
|
2020-01-04 21:16:37 +01:00
|
|
|
# 'cogs.Monitoring',
|
2020-01-15 00:08:53 +01:00
|
|
|
'cogs.Poll',
|
2020-01-04 02:46:12 +01:00
|
|
|
'cogs.Useful',
|
|
|
|
'cogs.User',
|
2019-09-09 22:40:17 +02:00
|
|
|
'jishaku',
|
2019-12-16 18:12:10 +01: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:
|
2020-04-01 16:38:34 +02:00
|
|
|
<<<<<<< HEAD
|
2020-04-01 16:37:03 +02:00
|
|
|
extras = [bot.cluster.get('Name') + '.']
|
2019-09-09 22:40:17 +02:00
|
|
|
if message.guild is not None:
|
2019-12-16 18:12:10 +01:00
|
|
|
if str(message.guild.id) in bot.prefixes:
|
|
|
|
extras.extend(
|
2019-12-17 22:41:54 +01:00
|
|
|
bot.prefixes.get(str(message.guild.id), "prefixes").split(
|
2019-12-29 23:18:10 +01:00
|
|
|
bot.config.get("misc", "Separator")
|
2019-12-17 22:41:54 +01:00
|
|
|
)
|
2019-12-16 18:12:10 +01:00
|
|
|
)
|
2020-04-01 16:38:34 +02:00
|
|
|
=======
|
2020-02-04 18:47:11 +01:00
|
|
|
try:
|
|
|
|
with open(f'./configs/guilds/{message.guild.id}.json', 'r') as f:
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
custom_prefix = data['prefixes']
|
|
|
|
except FileNotFoundError:
|
|
|
|
custom_prefix = ['']
|
|
|
|
|
|
|
|
extras = [bot.cluster.get('Name') + '.']
|
|
|
|
extras.extend(custom_prefix)
|
2020-04-01 16:38:34 +02:00
|
|
|
>>>>>>> cce7bb409303e9ad27ef4e5617d0bc9068810f13
|
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):
|
|
|
|
|
2020-02-04 18:47:11 +01:00
|
|
|
def __init__(self, ):
|
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-12-16 18:12:10 +01:00
|
|
|
self.config = Config('./configs/config.cfg')
|
|
|
|
self.blacklist = Config('./configs/blacklist.cfg')
|
2020-01-04 02:46:12 +01:00
|
|
|
self.fallbacks = Config('./configs/fallbacks.cfg')
|
|
|
|
self.cluster = self.fallbacks.find('True', key='This', first=True)
|
2019-09-09 22:40:17 +02:00
|
|
|
|
2020-01-15 22:56:54 +01:00
|
|
|
self.uptime: datetime = datetime.datetime.utcnow()
|
|
|
|
self._prev_events = deque(maxlen=10)
|
|
|
|
self.session = aiohttp.ClientSession(loop=self.loop)
|
2020-02-04 18:47:11 +01:00
|
|
|
|
|
|
|
self.database, self.metadata = database, metadata
|
|
|
|
self.engine = sqlalchemy.create_engine(str(self.database.url))
|
|
|
|
self.metadata.create_all(self.engine)
|
2020-01-15 22:56:54 +01:00
|
|
|
|
2020-01-15 01:23:53 +01:00
|
|
|
self.version = Version(*version, pre_release='rc2', build=build)
|
2020-02-04 18:47:11 +01:00
|
|
|
self.owners_id = [int(owner_id) for owner_id in self.config.get('permissions', 'Owners').split(', ')]
|
|
|
|
self.owner_id = int(self.owners_id[0])
|
2019-09-10 23:20:56 +02:00
|
|
|
|
2019-05-30 00:59:20 +02:00
|
|
|
for extension in l_extensions:
|
2019-12-16 18:12:10 +01:00
|
|
|
try:
|
2019-12-29 21:38:59 +01:00
|
|
|
self.load_extension(extension)
|
2019-12-16 18:12:10 +01:00
|
|
|
print(Texts().get("Extension loaded successfully : ")
|
|
|
|
+ extension)
|
|
|
|
log.info(Texts().get("Extension loaded successfully : ")
|
|
|
|
+ extension)
|
|
|
|
except Exception as e:
|
|
|
|
print(Texts().get("Failed to load extension : ")
|
|
|
|
+ extension, file=sys.stderr)
|
2019-12-29 21:38:59 +01:00
|
|
|
print(e)
|
2019-12-16 18:12:10 +01:00
|
|
|
log.error(Texts().get("Failed to load extension : ")
|
|
|
|
+ extension, exc_info=e)
|
2019-09-09 22:40:17 +02:00
|
|
|
|
2020-02-04 18:47:11 +01:00
|
|
|
@property
|
|
|
|
def owner(self):
|
|
|
|
return self.get_user(self.owner_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def owners(self):
|
|
|
|
return [self.get_user(owner_id) for owner_id in self.owners_id]
|
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def is_owner(self, user: discord.User) -> bool:
|
2020-02-04 18:47:11 +01:00
|
|
|
return user in self.owners
|
2019-09-10 18:23:24 +02:00
|
|
|
|
2020-01-15 21:40:12 +01:00
|
|
|
async def get_context(self, message, *, cls=None):
|
|
|
|
return await super().get_context(message, cls=cls or ContextPlus)
|
2019-09-10 18:23:24 +02:00
|
|
|
|
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
|
|
|
)
|
2020-01-15 22:56:54 +01:00
|
|
|
elif isinstance(error, commands.CommandOnCooldown):
|
|
|
|
await ctx.send(str(error))
|
2019-09-10 18:37:38 +02:00
|
|
|
|
2019-09-10 18:23:24 +02:00
|
|
|
async def process_commands(self, message: discord.message):
|
2020-01-15 21:40:12 +01:00
|
|
|
ctx: commands.Context = await self.get_context(message)
|
2019-09-09 22:40:17 +02:00
|
|
|
|
|
|
|
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-12-30 00:48:11 +01:00
|
|
|
if message.author.id in self.blacklist \
|
|
|
|
or (message.guild is not None
|
|
|
|
and message.guild.id in self.blacklist):
|
2019-12-29 21:38:59 +01:00
|
|
|
return
|
|
|
|
|
2019-12-30 00:48:11 +01:00
|
|
|
if message.author.bot and message.author.id != int(
|
|
|
|
self.config.get('bot', 'Tester')):
|
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-12-31 00:52:17 +01:00
|
|
|
print('-' * 60)
|
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)
|
2019-12-16 18:12:10 +01:00
|
|
|
if self.config.get("bot", "Activity", fallback=None) is not None:
|
|
|
|
presence.update(
|
|
|
|
activity=discord.Game(
|
|
|
|
name=self.config.get("bot", "Activity")
|
|
|
|
)
|
|
|
|
)
|
2019-12-31 00:52:17 +01:00
|
|
|
print(f"Discord.py: {discord.__version__}")
|
2020-01-01 23:44:46 +01:00
|
|
|
print(f"Server: {self.cluster.get('Name')}")
|
2019-12-31 00:52:17 +01:00
|
|
|
print('-' * 60)
|
2019-09-10 18:23:24 +02:00
|
|
|
|
|
|
|
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-12-31 00:52:17 +01:00
|
|
|
webhook_config = self.config["webhook"]
|
2019-10-06 01:49:30 +02:00
|
|
|
webhook = discord.Webhook.partial(
|
2019-12-31 00:52:17 +01:00
|
|
|
id=webhook_config.get('ID'),
|
|
|
|
token=webhook_config.get('Token'),
|
2019-10-06 01:49:30 +02:00
|
|
|
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):
|
2019-12-31 00:52:17 +01:00
|
|
|
extensions = self.extensions.copy()
|
|
|
|
for extension in extensions:
|
|
|
|
self.unload_extension(extension)
|
2019-09-08 23:05:43 +02:00
|
|
|
await super().close()
|
2019-12-16 18:12:10 +01:00
|
|
|
await self.session.close()
|
2019-05-30 00:59:20 +02:00
|
|
|
|
|
|
|
def run(self):
|
2019-12-16 18:12:10 +01:00
|
|
|
super().run(self.config.get("bot", "Token"), reconnect=True)
|
|
|
|
|
|
|
|
|
2019-12-16 18:19:32 +01:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def setup_logging():
|
2020-01-15 22:56:54 +01:00
|
|
|
logging.getLogger('discord').setLevel(logging.INFO)
|
|
|
|
logging.getLogger('discord.http').setLevel(logging.WARNING)
|
2019-12-16 18:19:32 +01:00
|
|
|
|
2020-01-15 22:56:54 +01:00
|
|
|
log = logging.getLogger()
|
|
|
|
log.setLevel(logging.INFO)
|
2019-12-16 18:19:32 +01:00
|
|
|
|
2020-01-15 22:56:54 +01:00
|
|
|
try:
|
2019-12-16 18:19:32 +01:00
|
|
|
handler = logging.FileHandler(filename='logs/tuxbot.log',
|
|
|
|
encoding='utf-8', mode='w')
|
|
|
|
fmt = logging.Formatter('[{levelname:<7}] [{asctime}]'
|
|
|
|
' {name}: {message}',
|
|
|
|
'%Y-%m-%d %H:%M:%S', style='{')
|
|
|
|
|
|
|
|
handler.setFormatter(fmt)
|
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
handlers = log.handlers[:]
|
|
|
|
for handler in handlers:
|
|
|
|
handler.close()
|
|
|
|
log.removeHandler(handler)
|
|
|
|
|
|
|
|
|
2019-12-16 18:12:10 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(Texts().get('Starting...'))
|
|
|
|
|
2020-01-15 22:56:54 +01:00
|
|
|
app = TuxBot()
|
2019-12-29 21:38:59 +01:00
|
|
|
|
2019-12-16 18:12:10 +01:00
|
|
|
try:
|
2019-12-16 18:19:32 +01:00
|
|
|
with setup_logging():
|
2020-01-15 22:56:54 +01:00
|
|
|
app.run()
|
2019-12-16 18:12:10 +01:00
|
|
|
except KeyboardInterrupt:
|
2020-01-15 22:56:54 +01:00
|
|
|
app.close()
|