2020-06-05 00:29:14 +02:00
|
|
|
import asyncio
|
|
|
|
import datetime
|
2020-06-04 19:16:51 +02:00
|
|
|
import logging
|
2020-06-05 00:29:14 +02:00
|
|
|
import sys
|
|
|
|
from typing import List, Union
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
import discord
|
2020-06-03 01:07:43 +02:00
|
|
|
from discord.ext import commands
|
2020-08-28 01:06:57 +02:00
|
|
|
from rich.traceback import install
|
|
|
|
|
2020-06-03 01:07:43 +02:00
|
|
|
from . import Config
|
2020-06-05 00:29:14 +02:00
|
|
|
from .data_manager import logs_data_path
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
from .utils.functions.cli import bordered
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-08-28 01:06:57 +02:00
|
|
|
from . import __version__, ExitCodes
|
2020-06-05 00:29:14 +02:00
|
|
|
from .utils.functions.extra import ContextPlus
|
2020-06-04 19:16:51 +02:00
|
|
|
|
2020-08-28 01:06:57 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
log = logging.getLogger("tuxbot")
|
2020-08-28 01:06:57 +02:00
|
|
|
install()
|
2020-06-04 19:16:51 +02:00
|
|
|
|
|
|
|
NAME = r"""
|
|
|
|
_____ _ _ _ _
|
|
|
|
|_ _| ___ _| |__ ___ | |_ | |__ ___ | |_
|
|
|
|
| || | | \ \/ / '_ \ / _ \| __|____| '_ \ / _ \| __|
|
|
|
|
| || |_| |> <| |_) | (_) | ||_____| |_) | (_) | |_
|
|
|
|
|_| \__,_/_/\_\_.__/ \___/ \__| |_.__/ \___/ \__|
|
|
|
|
"""
|
|
|
|
|
2020-06-11 01:06:30 +02:00
|
|
|
packages: List[str] = ["jishaku", "tuxbot.cogs.warnings", "tuxbot.cogs.admin"]
|
2020-06-03 01:07:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Tux(commands.AutoShardedBot):
|
2020-06-05 00:29:14 +02:00
|
|
|
_loading: asyncio.Task
|
|
|
|
|
|
|
|
def __init__(self, *args, cli_flags=None, **kwargs):
|
2020-06-03 19:41:30 +02:00
|
|
|
# by default, if the bot shutdown without any intervention,
|
|
|
|
# it's a crash
|
2020-06-05 00:29:14 +02:00
|
|
|
self.shutdown_code = ExitCodes.CRITICAL
|
2020-06-04 00:14:50 +02:00
|
|
|
self.cli_flags = cli_flags
|
|
|
|
self.instance_name = self.cli_flags.instance_name
|
|
|
|
self.last_exception = None
|
2020-06-05 00:29:14 +02:00
|
|
|
self.logs = logs_data_path(self.instance_name)
|
2020-06-03 19:41:30 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
self.config = Config(self.instance_name)
|
|
|
|
|
|
|
|
async def _prefixes(bot, message) -> List[str]:
|
2020-06-06 18:51:47 +02:00
|
|
|
prefixes = self.config("core").get("prefixes")
|
2020-06-05 00:29:14 +02:00
|
|
|
|
|
|
|
prefixes.extend(self.config.get_prefixes(message.guild))
|
2020-06-04 19:16:51 +02:00
|
|
|
|
2020-06-06 18:51:47 +02:00
|
|
|
if self.config("core").get("mentionable"):
|
2020-06-04 19:16:51 +02:00
|
|
|
return commands.when_mentioned_or(*prefixes)(bot, message)
|
|
|
|
return prefixes
|
|
|
|
|
|
|
|
if "command_prefix" not in kwargs:
|
|
|
|
kwargs["command_prefix"] = _prefixes
|
2020-06-03 19:41:30 +02:00
|
|
|
|
|
|
|
if "owner_ids" in kwargs:
|
|
|
|
kwargs["owner_ids"] = set(kwargs["owner_ids"])
|
|
|
|
else:
|
2020-06-05 00:29:14 +02:00
|
|
|
kwargs["owner_ids"] = self.config.owners_id()
|
2020-06-03 19:41:30 +02:00
|
|
|
|
|
|
|
message_cache_size = 100_000
|
|
|
|
kwargs["max_messages"] = message_cache_size
|
2020-06-04 00:14:50 +02:00
|
|
|
self.max_messages = message_cache_size
|
2020-06-03 19:41:30 +02:00
|
|
|
|
2020-06-04 00:14:50 +02:00
|
|
|
self.uptime = None
|
2020-06-05 00:29:14 +02:00
|
|
|
self._app_owners_fetched = False # to prevent abusive API calls
|
2020-06-04 00:14:50 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
super().__init__(*args, help_command=None, **kwargs)
|
2020-06-04 00:14:50 +02:00
|
|
|
|
2020-06-05 00:29:14 +02:00
|
|
|
async def load_packages(self):
|
|
|
|
if packages:
|
|
|
|
print("Loading packages...")
|
|
|
|
for package in packages:
|
|
|
|
try:
|
|
|
|
self.load_extension(package)
|
|
|
|
except Exception as e:
|
2020-06-06 18:51:47 +02:00
|
|
|
print(
|
|
|
|
Fore.RED
|
|
|
|
+ f"Failed to load package {package}"
|
|
|
|
+ Style.RESET_ALL
|
|
|
|
+ f" check "
|
|
|
|
f"{str((self.logs / 'tuxbot.log').resolve())} "
|
|
|
|
f"for more details"
|
2020-06-05 00:29:14 +02:00
|
|
|
)
|
|
|
|
|
2020-06-06 18:51:47 +02:00
|
|
|
log.exception(f"Failed to load package {package}", exc_info=e)
|
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
async def on_ready(self):
|
2020-06-05 00:29:14 +02:00
|
|
|
self.uptime = datetime.datetime.now()
|
2020-06-04 19:16:51 +02:00
|
|
|
INFO = {
|
2020-06-06 18:51:47 +02:00
|
|
|
"title": "INFO",
|
|
|
|
"rows": [
|
2020-06-04 19:16:51 +02:00
|
|
|
str(self.user),
|
|
|
|
f"Prefixes: {', '.join(self.config('core').get('prefixes'))}",
|
|
|
|
f"Language: {self.config('core').get('locale')}",
|
|
|
|
f"Tuxbot Version: {__version__}",
|
|
|
|
f"Discord.py Version: {discord.__version__}",
|
2020-06-06 18:51:47 +02:00
|
|
|
"Python Version: " + sys.version.replace("\n", ""),
|
2020-06-04 19:16:51 +02:00
|
|
|
f"Shards: {self.shard_count}",
|
|
|
|
f"Servers: {len(self.guilds)}",
|
2020-06-06 18:51:47 +02:00
|
|
|
f"Users: {len(self.users)}",
|
|
|
|
],
|
2020-06-04 19:16:51 +02:00
|
|
|
}
|
2020-06-03 19:41:30 +02:00
|
|
|
|
2020-06-06 18:51:47 +02:00
|
|
|
COGS = {"title": "COGS", "rows": []}
|
2020-06-05 00:29:14 +02:00
|
|
|
for extension in packages:
|
2020-06-06 18:51:47 +02:00
|
|
|
COGS["rows"].append(
|
2020-06-04 19:16:51 +02:00
|
|
|
f"[{'X' if extension in self.extensions else ' '}] {extension}"
|
|
|
|
)
|
|
|
|
|
|
|
|
print(Fore.LIGHTBLUE_EX + NAME)
|
|
|
|
print(Style.RESET_ALL)
|
|
|
|
print(bordered(INFO, COGS))
|
|
|
|
|
|
|
|
print(f"\n{'=' * 118}\n\n")
|
2020-06-05 00:29:14 +02:00
|
|
|
|
|
|
|
async def is_owner(self, user: Union[discord.User, discord.Member]) -> bool:
|
|
|
|
"""Determines if the user is a bot owner.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
user: Union[discord.User, discord.Member]
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
bool
|
|
|
|
"""
|
|
|
|
if user.id in self.config.owners_id():
|
|
|
|
return True
|
|
|
|
|
|
|
|
owner = False
|
|
|
|
if not self._app_owners_fetched:
|
|
|
|
app = await self.application_info()
|
|
|
|
if app.team:
|
|
|
|
ids = [m.id for m in app.team.members]
|
2020-06-06 18:51:47 +02:00
|
|
|
await self.config.update("core", "owners_id", ids)
|
2020-06-05 00:29:14 +02:00
|
|
|
owner = user.id in ids
|
|
|
|
self._app_owners_fetched = True
|
|
|
|
|
|
|
|
return owner
|
|
|
|
|
|
|
|
async def get_context(self, message: discord.Message, *, cls=None):
|
|
|
|
return await super().get_context(message, cls=ContextPlus)
|
|
|
|
|
|
|
|
async def process_commands(self, message: discord.Message):
|
|
|
|
"""Check for blacklists.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if message.author.bot:
|
|
|
|
return
|
|
|
|
|
2020-06-06 18:51:47 +02:00
|
|
|
if (
|
|
|
|
message.guild.id in self.config.get_blacklist("guild")
|
|
|
|
or message.channel.id in self.config.get_blacklist("channel")
|
|
|
|
or message.author.id in self.config.get_blacklist("user")
|
|
|
|
):
|
2020-06-05 00:29:14 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
ctx = await self.get_context(message)
|
|
|
|
|
|
|
|
if ctx is None or ctx.valid is False:
|
|
|
|
self.dispatch("message_without_command", message)
|
|
|
|
else:
|
|
|
|
await self.invoke(ctx)
|
|
|
|
|
|
|
|
async def on_message(self, message: discord.Message):
|
|
|
|
await self.process_commands(message)
|
|
|
|
|
|
|
|
async def logout(self):
|
|
|
|
"""Disconnect from Discord and closes all actives connections.
|
|
|
|
|
|
|
|
Todo: add postgresql logout here
|
|
|
|
"""
|
|
|
|
await super().logout()
|
|
|
|
|
|
|
|
async def shutdown(self, *, restart: bool = False):
|
|
|
|
"""Gracefully quit.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
restart:bool
|
|
|
|
If `True`, systemd or the launcher gonna see custom exit code
|
|
|
|
and reboot.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not restart:
|
|
|
|
self.shutdown_code = ExitCodes.SHUTDOWN
|
|
|
|
else:
|
|
|
|
self.shutdown_code = ExitCodes.RESTART
|
|
|
|
|
|
|
|
await self.logout()
|
|
|
|
sys.exit(self.shutdown_code)
|