Skip to content

Commit

Permalink
Migrate AoC features from Sir-Lancebot
Browse files Browse the repository at this point in the history
Co-Authored-By: Janine vN <[email protected]>
Co-Authored-By: ChrisJL <[email protected]>
Co-Authored-By: Ben Soyka <[email protected]>
Co-Authored-By: TizzySaurus <[email protected]>
Co-Authored-By: Hassan Abouelela <[email protected]>
Co-Authored-By: Matteo Bertucci <[email protected]>
Co-Authored-By: Johannes Christ <[email protected]>
Co-Authored-By: arl <[email protected]>
Co-Authored-By: Sebastiaan Zeeff <[email protected]>
Co-Authored-By: Xithrius <[email protected]>
Co-Authored-By: wookie184 <[email protected]>
Co-Authored-By: Joe Banks <[email protected]>
Co-Authored-By: ToxicKidz <[email protected]>
Co-Authored-By: decorator-factory <[email protected]>
  • Loading branch information
15 people committed Mar 6, 2022
1 parent 36977ce commit c2a7402
Show file tree
Hide file tree
Showing 19 changed files with 2,769 additions and 85 deletions.
64 changes: 61 additions & 3 deletions bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
import socket
from typing import Optional

import aiohttp
import disnake
from async_rediscache import RedisSession
from botcore.utils.logging import get_logger
from botcore.utils.scheduling import create_task
from disnake.ext import commands
Expand All @@ -13,9 +17,11 @@
class SirRobin(commands.Bot):
"""Sir-Robin core."""

def __init__(self, **kwargs):
def __init__(self, redis_session: RedisSession, **kwargs):
super().__init__(**kwargs)
self._guild_available = asyncio.Event()
self.http_session: Optional[aiohttp.ClientSession] = None
self.redis_session = redis_session
create_task(self.check_channels(), event_loop=self.loop)
create_task(self.send_log(constants.Client.name, "Connected!"), event_loop=self.loop)

Expand Down Expand Up @@ -96,5 +102,57 @@ async def wait_until_guild_available(self) -> None:
"""
await self._guild_available.wait()


bot = SirRobin(command_prefix=constants.Client.prefix, activity=disnake.Game("The Not-Quite-So-Bot-as-Sir-Lancebot"))
async def login(self, *args, **kwargs) -> None:
"""Re-create the connector and set up sessions before logging into Discord."""
# Use asyncio for DNS resolution instead of threads so threads aren't spammed.
self._resolver = aiohttp.AsyncResolver()

# Use AF_INET as its socket family to prevent HTTPS related problems both locally
# and in production.
self._connector = aiohttp.TCPConnector(
resolver=self._resolver,
family=socket.AF_INET,
)

# Client.login() will call HTTPClient.static_login() which will create a session using
# this connector attribute.
self.http.connector = self._connector

self.http_session = aiohttp.ClientSession(connector=self._connector)

await super().login(*args, **kwargs)

async def close(self) -> None:
"""Close Redis session when bot is shutting down."""
await super().close()

if self.http_session:
await self.http_session.close()

if self.redis_session:
await self.redis_session.close()


intents = disnake.Intents.all()
intents.presences = False
intents.dm_typing = False
intents.dm_reactions = False
intents.invites = False
intents.webhooks = False
intents.integrations = False

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="sir-lancebot"
)

bot = SirRobin(
redis_session=redis_session,
command_prefix=constants.Client.prefix,
activity=disnake.Game("The Not-Quite-So-Bot-as-Sir-Lancebot"),
intents=intents
)
Loading

0 comments on commit c2a7402

Please sign in to comment.