REF: Created the required files for the bot

FEAT: added the basic .env as well as a sample.env to allow people to clone
FEAT: Stores birthdays in SQL.
REF: Still need to add GDPR and other privacy related code.
This commit is contained in:
Dan 2024-08-11 18:54:05 -04:00
parent 98455273b6
commit efc4ed6baf
7 changed files with 220 additions and 0 deletions

15
.github/workflows/discord_sync.yml vendored Normal file
View File

@ -0,0 +1,15 @@
name: Discord Webhook
on: [push]
jobs:
git:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run Discord Webhook
uses: johnnyhuy/actions-discord-git-webhook@main
with:
webhook_url: ${{ secrets.YOUR_DISCORD_WEBHOOK_URL }}

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "E://Development//Kenny Projects//Eona//main.py",
"console": "integratedTerminal"
}
]
}

113
birthday.py Normal file
View File

@ -0,0 +1,113 @@
import discord
from discord.ext import tasks
import sqlite3
from logger import logger
import datetime
class Birthday:
def __init__(self, client):
self.client = client
self.db_path = "data/EONA.db"
self.logger = logger
self.ensure_db()
def ensure_db(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS birthdays (
user_id INTEGER,
guild_id INTEGER,
user_name TEXT,
birthday TEXT,
PRIMARY KEY (user_id, guild_id)
);""")
conn.commit()
conn.close()
self.logger.info("Birthday table ensured")
async def set_birthday(self, user_id, guild_id, birthday):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""INSERT INTO birthdays (user_id, guild_id, birthday)
VALUES (?, ?, ?) ON CONFLICT(user_id, guild_id)
DO UPDATE SET birthday = excluded.birthday;""",
(user_id, guild_id, birthday))
conn.commit()
conn.close()
self.logger.info(f"{user_id}! Your birthday was set to {birthday}")
async def get_birthday(self, user_id, guild_id):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT birthday FROM birthdays WHERE user_id = ? AND guild_id = ?", (user_id, guild_id))
row = cursor.fetchone()
conn.close()
return row[0] if row else None
@tasks.loop(hours=24)
async def check_birthdays(self):
today = datetime.date.today().strftime("%Y-%m-%d")
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT user_id, guild_id FROM birthdays where birthday = ?", (today,))
rows = cursor.fetchall()
conn.close()
for user_id, guild_id in rows:
guild = self.client.get_guild(int(guild_id))
if guild:
user = guild.get_member(int(user_id))
if user:
channel = guild.system_channel or next((ch for ch in guild.text_channels if ch.permissions_for(guild.me).send_messages), None)
if channel:
await channel.send(f"Happy birthday {user.mention}! Happy birthday! 🎉🎂")
async def setup_hook(self):
self.check_birthdays.start()
self.logger.info("Birthday module loaded")
def setup(self, tree: discord.app_commands.CommandTree):
@tree.command(name="set_birthday", description="Set your birthday (YYYY-MM-DD)")
async def set_birthday_command(interaction: discord.Interaction):
await interaction.response.send_modal(BirthdayModal(self))
@tree.command(name="get_birthday", description="Check your birthday")
async def get_birthday_command(interaction: discord.Interaction):
user_id = str(interaction.user.id)
guild_id = str(interaction.guild.id)
birthday = await self.get_birthday(user_id, guild_id)
if birthday:
await interaction.response.send_message(f"Your birthday has been set to {birthday}", ephemeral=True)
else:
await interaction.response.send_message("Your birthday has not been set. Please use `/set_birthday` to set your birthday.", ephemeral=True)
if not tree.get_command("set_birthday"):
tree.add_command(set_birthday_command)
if not tree.get_command("get_birthday"):
tree.add_command(get_birthday_command)
class BirthdayModal(discord.ui.Modal, title="Set your birthday"):
birthday = discord.ui.TextInput(label="Your birthday", placeholder="YYYY-MM-DD", style=discord.TextStyle.short, required=True)
def __init__(self, birthday_module):
super().__init__()
self.birthday_module = birthday_module
async def on_submit(self, interaction: discord.Interaction):
user_id = str(interaction.user.id)
guild_id = str(interaction.guild.id)
try:
birthday = self.birthday.value
await self.birthday_module.set_birthday(user_id, guild_id, birthday)
await interaction.response.send_message(f"Your birthday has been set to {birthday}", ephemeral=True)
except ValueError as e:
await interaction.response.send_message(str(e), ephemeral=True)
def setup(client):
birthday_module = Birthday(client)
client.add_cog(birthday_module)
client.birthday_module = birthday_module

11
config.py Normal file
View File

@ -0,0 +1,11 @@
from dotenv import load_dotenv
import os
load_dotenv()
config = {
"TOKEN": os.getenv("DISCORD_TOKEN"),
"modules": {
'birthday': {'enabled': True},
}
}

19
logger.py Normal file
View File

@ -0,0 +1,19 @@
import logging
from logging.handlers import RotatingFileHandler
logging.basicConfig(
level=logging.ERROR,
format="%(asctime)s - %(name)s - " "%(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log_file = "logs/EONA.log"
handler = RotatingFileHandler(log_file, maxBytes=1e6, backupCount=5)
formatter = logging.Formatter(
"%(asctime)s:%(levelname)s:%(name)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger("EONA")
logger.addHandler(handler)
logger.setLevel(logging.INFO)

45
main.py Normal file
View File

@ -0,0 +1,45 @@
from config import config
import discord
from logger import logger
logger.info("Starting EONA...")
TOKEN = config["TOKEN"]
intents = discord.Intents.default()
class EONA(discord.Client):
def __init__(self):
super().__init__(intents=intents)
self.tree = discord.app_commands.CommandTree(self)
self.load_modules()
async def setup_hook(self):
logger.info("Setting up...")
await self.tree.sync()
logger.info("Commands Synced")
def load_modules(self):
try:
from config import config
except ImportError:
logger.error("config.py not found")
try:
if config["modules"]["birthday"]["enabled"]:
from birthday import Birthday
birthday = Birthday(self)
birthday.setup(self.tree)
logger.info("Birthday module loaded")
except KeyError:
logger.error("Birthday module not enabled")
client = EONA()
@client.event
async def on_ready():
print(f"Logged in as {client.user} (ID: {client.user.id})")
client.run(TOKEN)

2
sample.env Normal file
View File

@ -0,0 +1,2 @@
# Move or copy sample.env to .env
DISCORD_TOKEN = "PUT YOUR TOKEN HERE"