Files
bot.py
README.md
requirements.txt
Code
3 1,982
bot.py
import asyncio
import json
import logging
import os
import re
import secrets
import sqlite3
import time
import urllib.error
import urllib.request
import discord
from discord import app_commands
from discord.ext import commands, tasks
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("project_octave")
ACTIVATION_CODE = os.getenv("ACTIVATION_CODE") or "@#kttmvas,mco)@4951-F03"
OWNER_ID = os.getenv("OWNER_ID", "").strip()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "").strip()
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini").strip()
CREDIT_UPDATE_TRIGGER = "0m0in[vim[g-2"
DEFAULT_AUTHOR_CREDIT = (
"# Project Octave | Bot Author Credit\n\n"
"Made by Yopait\n\n"
"# Yopait | Hub\n\n"
"This is my Offcial Channel Hub for my Youtube\n"
"https://discord.gg/feUtcSz8Bn\n"
"https://www.youtube.com/@YopaitYT"
)
credit_update_users: set[int] = set()
PROJECT_OCTAVE_AD = (
os.getenv("PROJECT_OCTAVE_AD")
or """# ✨ Project Octave ✨
# Event Schedule 📅
No Current Events Scheduled
in ✨ Project Octave ✨ but
will be hosting more soon.
# About The Event ⁉️
The Dweller Civilization Event is a civilization event, but not any normal one! Everybody is split Into 2 Islands where there will be custom structures and biomes.
# Other Benefits 👍
🤝 ┃Partnerships
🚀 ┃Mini-Events
🎮 ┃Game Nights
💎 ┃Friendships
☘️ ┃Giveaways
👔 ┃Staff Credit
# Application Process📝
We don’t like the same old Video Application process so we use a Google Form To Apply. With a Lower player count our application standards are lower then ever!
━━━━━━━━━━━━━━━━━━
🔗 Discord Server : https://discord.gg/WFgqyQKHWw
━━━━━━━━━━━━━━━━━━
# Join Project Octave for Events Today, And Apply For Current Events Available
|| @everyone||"""
).replace("\\n", "\n")
DATABASE_PATH = os.getenv("DATABASE_PATH", "octave.sqlite3")
DB: sqlite3.Connection | None = None
def initialize_database() -> None:
global DB
DB = sqlite3.connect(DATABASE_PATH)
DB.row_factory = sqlite3.Row
DB.execute("PRAGMA foreign_keys = ON")
version = DB.execute("PRAGMA user_version").fetchone()[0]
if version > 3:
raise RuntimeError("The database schema is newer than this bot supports.")
DB.executescript(
"""
CREATE TABLE IF NOT EXISTS guild_settings (
guild_id INTEGER PRIMARY KEY,
partnership_channel_id INTEGER,
panel_channel_id INTEGER,
partnership_log_channel_id INTEGER,
request_category_id INTEGER,
staff_role_id INTEGER,
head_staff_role_id INTEGER,
completed INTEGER NOT NULL DEFAULT 0,
panel_message_id INTEGER,
leaderboard_channel_id INTEGER,
leaderboard_message_id INTEGER,
leaderboard_page INTEGER NOT NULL DEFAULT 0,
leaderboard_page_changed_at REAL NOT NULL DEFAULT 0,
botstalk_channel_id INTEGER
);
CREATE TABLE IF NOT EXISTS bot_settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS partnership_requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id INTEGER NOT NULL,
requester_id INTEGER NOT NULL,
channel_id INTEGER,
status TEXT NOT NULL,
ad_text TEXT,
ad_attachments TEXT NOT NULL DEFAULT '[]',
proof_attachments TEXT NOT NULL DEFAULT '[]',
reported_count INTEGER,
count_source TEXT NOT NULL DEFAULT 'reported',
proof_skipped_by INTEGER,
review_message_id INTEGER,
created_at REAL NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS one_open_request_per_user
ON partnership_requests(guild_id, requester_id) WHERE status <> 'closed';
CREATE INDEX IF NOT EXISTS request_channel_lookup
ON partnership_requests(guild_id, channel_id, status);
CREATE TABLE IF NOT EXISTS staff_points (
guild_id INTEGER NOT NULL,
staff_id INTEGER NOT NULL,
points INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (guild_id, staff_id)
);
CREATE TABLE IF NOT EXISTS warnings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id INTEGER NOT NULL,
target_type TEXT NOT NULL,
target_id INTEGER NOT NULL,
active INTEGER NOT NULL DEFAULT 1,
reason TEXT NOT NULL,
issued_by INTEGER NOT NULL,
created_at REAL NOT NULL,
resolved_by INTEGER,
resolved_at REAL
);
CREATE INDEX IF NOT EXISTS active_warning_lookup
ON warnings(guild_id, active, target_type, target_id);
CREATE TABLE IF NOT EXISTS warning_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id INTEGER NOT NULL,
target_type TEXT NOT NULL,
target_id INTEGER NOT NULL,
action TEXT NOT NULL,
actor_id INTEGER NOT NULL,
reason TEXT NOT NULL,
created_at REAL NOT NULL
);
"""
)
DB.execute(
"INSERT OR IGNORE INTO bot_settings(key, value) VALUES ('author_credit', ?)",
(DEFAULT_AUTHOR_CREDIT,),
)
DB.execute(
"UPDATE bot_settings SET value = ? WHERE key = 'author_credit' AND value = ?",
(
DEFAULT_AUTHOR_CREDIT,
"# Project Octave | Bot Author\n\nThis Bot Project was Created by Yopait",
),
)
settings_columns = {
row[1] for row in DB.execute("PRAGMA table_info(guild_settings)").fetchall()
}
if "partnership_log_channel_id" not in settings_columns:
DB.execute("ALTER TABLE guild_settings ADD COLUMN partnership_log_channel_id INTEGER")
if "panel_channel_id" not in settings_columns:
DB.execute("ALTER TABLE guild_settings ADD COLUMN panel_channel_id INTEGER")
DB.execute(
"UPDATE guild_settings SET panel_channel_id = partnership_channel_id "
"WHERE panel_message_id IS NOT NULL"
)
request_columns = {
row[1] for row in DB.execute("PRAGMA table_info(partnership_requests)").fetchall()
}
if "count_source" not in request_columns:
DB.execute(
"ALTER TABLE partnership_requests ADD COLUMN count_source TEXT NOT NULL DEFAULT 'reported'"
)
if "request_category_id" not in settings_columns:
DB.execute("ALTER TABLE guild_settings ADD COLUMN request_category_id INTEGER")
if "botstalk_channel_id" not in settings_columns:
DB.execute("ALTER TABLE guild_settings ADD COLUMN botstalk_channel_id INTEGER")
if version < 3:
DB.execute("PRAGMA user_version = 3")
DB.commit()
# A process interruption during a review decision must not permanently lock the request.
DB.execute(
"UPDATE partnership_requests SET status = 'awaiting_review' "
"WHERE status = 'finalizing'"
)
DB.commit()
def database() -> sqlite3.Connection:
if DB is None: