Files
WheatlyBot/data/react_roles.py

145 lines
4.2 KiB
Python

import sqlite3
DB_PATH = 'data.db'
def init_db():
"""
Initialize the database and create necessary tables.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Create the react_roles table
cursor.execute('''
CREATE TABLE IF NOT EXISTS react_roles (
message_id INTEGER,
role_id INTEGER,
emoji TEXT,
description TEXT
)
''')
# Create the react_role_categories table
cursor.execute('''
CREATE TABLE IF NOT EXISTS react_role_categories (
message_id INTEGER,
category_name TEXT
)
''')
conn.commit()
conn.close()
def add_react_role_category_to_db(message_id: int, category_name: str):
"""
Add a new react role category to the database.
:param message_id: The ID of the message to which the category is associated.
:param category_name: The name of the category (e.g., "Category1").
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS react_role_categories (
message_id INTEGER,
category_name TEXT
)
''')
# Insert the new react-role category into the database
cursor.execute('''
INSERT INTO react_role_categories (message_id, category_name)
VALUES (?, ?)
''', (message_id, category_name.lower()))
conn.commit()
conn.close()
def add_react_role_to_db(message_id: int, role_id: int, emoji: str, description: str):
"""
Add a new react role to the database.
:param message_id: The message ID of the react-role message.
:param role_id: The ID of the role to assign.
:param emoji: The emoji to react with.
:param description: The description of the role.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Create the table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS react_roles (
message_id INTEGER,
role_id INTEGER,
emoji TEXT,
description TEXT
)
''')
# Insert the new react-role into the database
cursor.execute('''
INSERT INTO react_roles (message_id, role_id, emoji, description)
VALUES (?, ?, ?, ?)
''', (message_id, role_id, emoji, description))
conn.commit()
conn.close()
def get_react_roles_categories():
"""
Get all react role categories from the database.
:return: A list of tuples containing message_id and category_name.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Fetch all react-role categories from the database
cursor.execute('''
SELECT * FROM react_role_categories
''')
categories = cursor.fetchall()
conn.close()
return categories
def get_react_roles_by_category(category_name: str):
"""
Get all react roles for a specific category from the database.
:param category_name: The name of the category (e.g., "Category1").
:return: A list of tuples containing message_id, role_id, emoji, and description.
"""
# Get the message id of the category
categories = get_react_roles_categories()
message_id = next((cat[0] for cat in categories if cat[1] == category_name.lower()), None)
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Fetch all react-roles for the specified category from the database
cursor.execute('''
SELECT * FROM react_roles WHERE message_id = ?
''', (message_id,))
roles = cursor.fetchall()
conn.close()
return roles
def get_react_roles_by_message_id(message_id: int):
"""
Get all react roles for a specific message ID from the database.
:param message_id: The ID of the message to which the roles are associated.
:return: A list of tuples containing message_id, role_id, emoji, and description.
"""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Fetch all react-roles for the specified message ID from the database
cursor.execute('''
SELECT * FROM react_roles WHERE message_id = ?
''', (message_id,))
roles = cursor.fetchall()
conn.close()
return roles