Initialize database stuff & setup didier

This commit is contained in:
stijndcl 2022-06-10 01:48:02 +02:00
parent 0701fbfddb
commit 3a35718d84
13 changed files with 305 additions and 14 deletions

View file

@ -0,0 +1 @@
from .didier import Didier

0
didier/data/__init__.py Normal file
View file

1
didier/data/constants.py Normal file
View file

@ -0,0 +1 @@
PREFIXES = ["didier", "big d"]

36
didier/didier.py Normal file
View file

@ -0,0 +1,36 @@
import discord
from discord import Message
from discord.ext import commands
from sqlalchemy.ext.asyncio import AsyncSession
import settings
from database.engine import DBSession
from didier.utils.prefix import get_prefix
class Didier(commands.Bot):
"""DIDIER <3"""
def __init__(self):
activity = discord.Activity(type=discord.ActivityType.playing, name=settings.DISCORD_STATUS_MESSAGE)
status = discord.Status.online
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
super().__init__(
command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status
)
@property
def db_session(self) -> AsyncSession:
"""Obtain a database session"""
return DBSession()
async def on_ready(self):
"""Event triggered when the bot is ready"""
print(settings.DISCORD_READY_MESSAGE)
async def on_message(self, message: Message, /) -> None:
print(message.content)

0
didier/utils/__init__.py Normal file
View file

27
didier/utils/prefix.py Normal file
View file

@ -0,0 +1,27 @@
import re
from discord import Message
from discord.ext import commands
from didier.data import constants
def get_prefix(client: commands.Bot, message: Message) -> str:
"""Match a prefix against a message
This is done dynamically to allow variable amounts of whitespace,
and through regexes to allow case-insensitivity among other things.
"""
mention = f"<@!{client.user.id}>"
regex = r"^({})\s*"
# Check which prefix was used
for prefix in [*constants.PREFIXES, mention]:
match = re.match(regex.format(prefix), message.content, flags=re.I)
if match is not None:
# Get the part of the message that was matched
# .group() is inconsistent with whitespace, so that can't be used
return message.content[: match.end()]
# Matched nothing
return "didier"