aboutsummaryrefslogtreecommitdiffstats
path: root/bot/__main__.py
blob: 418fd91b5bd6653cba5cb1c3c4cc29e4dba7d4e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
import logging

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

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),
        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)


asyncio.run(main())