diff options
author | 2022-09-23 22:58:49 +0100 | |
---|---|---|
committer | 2022-09-23 22:58:49 +0100 | |
commit | f9cc77f55a7bac9cff1f5674b36b3f17560f6bfe (patch) | |
tree | 4d9a9684d6c0d8f1f749355353fbadb7fd89960b /bot/__main__.py | |
parent | Fix issue #1050 (#1097) (diff) | |
parent | Remove all wait_until_guil_available as this is now done in bot-core (diff) |
Merge pull request #1092 from python-discord/bot-core-migration
Diffstat (limited to 'bot/__main__.py')
-rw-r--r-- | bot/__main__.py | 90 |
1 files changed, 79 insertions, 11 deletions
diff --git a/bot/__main__.py b/bot/__main__.py index bd6c70ee..9cf63dc5 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,19 +1,87 @@ +import asyncio import logging -from bot.bot import bot -from bot.constants import Client, STAFF_ROLES, WHITELISTED_CHANNELS +import aiohttp +import discord +from async_rediscache import RedisSession +from botcore import StartupError +from discord.ext import commands +from redis import RedisError + +import bot +from bot import constants +from bot.bot import Bot from bot.utils.decorators import whitelist_check -from bot.utils.extensions import walk_extensions log = logging.getLogger(__name__) -bot.add_check(whitelist_check(channels=WHITELISTED_CHANNELS, roles=STAFF_ROLES)) -for ext in walk_extensions(): - bot.load_extension(ext) +async def _create_redis_session() -> RedisSession: + """Create and connect to a redis session.""" + redis_session = RedisSession( + host=constants.RedisConfig.host, + port=constants.RedisConfig.port, + password=constants.RedisConfig.password, + max_connections=20, + use_fakeredis=constants.RedisConfig.use_fakeredis, + global_namespace="bot", + decode_responses=True, + ) + try: + return await redis_session.connect() + except RedisError as e: + raise StartupError(e) + + +async def test_bot_in_ci(bot: Bot) -> None: + """ + Attempt to import all extensions and then return. + + This is to ensure that all extensions can at least be + imported and have a setup function within our CI. + """ + from botcore.utils._extensions import walk_extensions + + from bot import exts + + for _ in walk_extensions(exts): + # walk_extensions does all the heavy lifting within the generator. + pass + + +async def main() -> None: + """Entry async method for starting the bot.""" + allowed_roles = list({discord.Object(id_) for id_ in constants.MODERATION_ROLES}) + intents = discord.Intents.default() + intents.bans = False + intents.integrations = False + intents.invites = False + intents.message_content = True + intents.typing = False + intents.webhooks = False + + async with aiohttp.ClientSession() as session: + bot.instance = Bot( + guild_id=constants.Client.guild, + http_session=session, + redis_session=await _create_redis_session(), + command_prefix=commands.when_mentioned_or(constants.Client.prefix), + activity=discord.Game(name=f"Commands: {constants.Client.prefix}help"), + case_insensitive=True, + allowed_mentions=discord.AllowedMentions(everyone=False, roles=allowed_roles), + intents=intents, + allowed_roles=allowed_roles, + ) + + async with bot.instance as _bot: + _bot.add_check(whitelist_check( + channels=constants.WHITELISTED_CHANNELS, + roles=constants.STAFF_ROLES, + )) + if constants.Client.in_ci: + await test_bot_in_ci(_bot) + else: + await _bot.start(constants.Client.token) + -if not Client.in_ci: - # Manually enable the message content intent. This is required until the below PR is merged - # https://github.com/python-discord/sir-lancebot/pull/1092 - bot._connection._intents.value += 1 << 15 - bot.run(Client.token) +asyncio.run(main()) |