From 510da5190a0f499397c9419fa3b125233bed566c Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 28 Jun 2022 22:36:22 +0100 Subject: Use BotBase from bot core --- bot/__main__.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 11 deletions(-) (limited to 'bot/__main__.py') diff --git a/bot/__main__.py b/bot/__main__.py index bd6c70ee..418fd91b 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,19 +1,67 @@ +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 + +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( + address=(constants.RedisConfig.host, constants.RedisConfig.port), + password=constants.RedisConfig.password, + minsize=1, + maxsize=20, + use_fakeredis=constants.RedisConfig.use_fakeredis, + global_namespace="bot", + ) + try: + await redis_session.connect() + except OSError as e: + raise StartupError(e) + return redis_session + + +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, + )) + 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()) -- cgit v1.2.3 From b1d5c4375256cf9ea4043edd41fd88a33b3df05d Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 23 Aug 2022 20:51:15 +0100 Subject: Update redis init due to new redis-py upgrade --- bot/__main__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'bot/__main__.py') diff --git a/bot/__main__.py b/bot/__main__.py index 418fd91b..5bff1bef 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -6,6 +6,7 @@ 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 @@ -18,18 +19,18 @@ log = logging.getLogger(__name__) async def _create_redis_session() -> RedisSession: """Create and connect to a redis session.""" redis_session = RedisSession( - address=(constants.RedisConfig.host, constants.RedisConfig.port), + host=constants.RedisConfig.host, + port=constants.RedisConfig.port, password=constants.RedisConfig.password, - minsize=1, - maxsize=20, + max_connections=20, use_fakeredis=constants.RedisConfig.use_fakeredis, global_namespace="bot", + decode_responses=True, ) try: - await redis_session.connect() - except OSError as e: + return await redis_session.connect() + except RedisError as e: raise StartupError(e) - return redis_session async def main() -> None: -- cgit v1.2.3 From a2b0e761cb6e61ec18c574479f267a009a638eae Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 18 Sep 2022 13:09:26 +0100 Subject: Support loading all extensions in CI --- bot/__main__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'bot/__main__.py') diff --git a/bot/__main__.py b/bot/__main__.py index 5bff1bef..9cf63dc5 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -33,6 +33,22 @@ async def _create_redis_session() -> RedisSession: 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}) @@ -62,7 +78,10 @@ async def main() -> None: channels=constants.WHITELISTED_CHANNELS, roles=constants.STAFF_ROLES, )) - await _bot.start(constants.Client.token) + if constants.Client.in_ci: + await test_bot_in_ci(_bot) + else: + await _bot.start(constants.Client.token) asyncio.run(main()) -- cgit v1.2.3