Persistently store custom commands
This commit is contained in:
parent
59a65f778e
commit
98a02adc2f
2 changed files with 22 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
||||||
|
# SQLite database
|
||||||
|
storage.db
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
|
|
19
bot.py
19
bot.py
|
@ -1,16 +1,31 @@
|
||||||
# Basic twitch bot
|
# Basic twitch bot
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sqlite3
|
||||||
from twitchio.ext import commands
|
from twitchio.ext import commands
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
PREFIX = "!"
|
PREFIX = "!"
|
||||||
|
DB_PATH = "storage.db"
|
||||||
|
|
||||||
|
|
||||||
class Bot(commands.Bot):
|
class Bot(commands.Bot):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(token = os.environ["TMI_TOKEN"], prefix=PREFIX, initial_channels=[os.environ["CHANNEL"]])
|
super().__init__(token = os.environ["TMI_TOKEN"], prefix=PREFIX, initial_channels=[os.environ["CHANNEL"]])
|
||||||
self.custom_commands = {}
|
self.custom_commands = {}
|
||||||
|
self.db_con = sqlite3.connect(DB_PATH)
|
||||||
|
self.db_cur = self.db_con.cursor()
|
||||||
|
|
||||||
|
if self.db_cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='custom_commands'").fetchone() == None:
|
||||||
|
print("Created database table")
|
||||||
|
self.db_cur.execute("CREATE TABLE custom_commands(command, output_text)")
|
||||||
|
|
||||||
|
commands = self.db_cur.execute("SELECT * FROM custom_commands").fetchall()
|
||||||
|
for command in commands:
|
||||||
|
self.custom_commands[command[0]] = command[1]
|
||||||
|
if len(self.custom_commands) > 0:
|
||||||
|
print("Loaded custom commands:")
|
||||||
|
print(self.custom_commands)
|
||||||
|
|
||||||
async def event_ready(self):
|
async def event_ready(self):
|
||||||
print(f"Logged in as | {self.nick}")
|
print(f"Logged in as | {self.nick}")
|
||||||
|
@ -66,6 +81,8 @@ class Bot(commands.Bot):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.custom_commands[command_name] = command_text
|
self.custom_commands[command_name] = command_text
|
||||||
|
self.db_cur.execute("INSERT INTO custom_commands VALUES (?, ?)", [command_name, command_text])
|
||||||
|
self.db_con.commit()
|
||||||
await ctx.send(f"Adding command: \"{PREFIX}{command_name}\" -> \"{command_text}\"")
|
await ctx.send(f"Adding command: \"{PREFIX}{command_name}\" -> \"{command_text}\"")
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
|
@ -89,6 +106,8 @@ class Bot(commands.Bot):
|
||||||
return
|
return
|
||||||
|
|
||||||
del self.custom_commands[command_name]
|
del self.custom_commands[command_name]
|
||||||
|
self.db_cur.execute("DELETE FROM custom_commands WHERE command=?", [command_name])
|
||||||
|
self.db_con.commit()
|
||||||
await ctx.send(f"Deleted command \"{PREFIX}{command_name}\"")
|
await ctx.send(f"Deleted command \"{PREFIX}{command_name}\"")
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
|
|
Loading…
Add table
Reference in a new issue