63 lines
1.6 KiB
Python
Executable File
63 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
This module allows you to add topics to a list in the database and cycle
|
|
through them.
|
|
"""
|
|
import os
|
|
import threading
|
|
import random
|
|
from sqlite3 import IntegrityError, OperationalError
|
|
|
|
from module import commands, example
|
|
|
|
def setup(bot):
|
|
"""
|
|
Attempts to create the table in the database if it's not found.
|
|
"""
|
|
bot.memory['topic_lock'] = threading.Lock()
|
|
con = bot.db.connect()
|
|
cur = con.cursor()
|
|
try:
|
|
cur.execute("SELECT * FROM topic").fetchone()
|
|
except OperationalError:
|
|
cur.execute("CREATE TABLE topic("
|
|
"topic TEXT PRIMARY KEY,"
|
|
"added_by TEXT,"
|
|
"added_date INTEGER DEFAULT (STRFTIME('%s', 'now')))")
|
|
con.commit()
|
|
con.close()
|
|
|
|
|
|
@commands('topic')
|
|
def topic(bot, trigger):
|
|
"""
|
|
Picks a random topic from the database and applies it.
|
|
"""
|
|
topic = bot.db.execute("SELECT topic FROM topic " \
|
|
"ORDER BY RANDOM() LIMIT 1;").fetchone()
|
|
if not topic:
|
|
return bot.reply("Topic database is empty!")
|
|
else:
|
|
topic = topic[0]
|
|
bot.topic(trigger.channel, topic)
|
|
|
|
|
|
@commands('addtopic')
|
|
@example(".addtopic We're discussing penises, would you like to join?")
|
|
def addTopic(bot, trigger):
|
|
"""
|
|
Adds the specified topic to the topic database.
|
|
"""
|
|
if len(trigger.args) < 2:
|
|
return bot.msg("Please be providing a topic sir.")
|
|
topic = trigger.args[1]
|
|
bot.memory['topic_lock'].acquire()
|
|
try:
|
|
bot.db.execute("INSERT INTO topic (topic, added_by) VALUES(?,?)",
|
|
(topic, trigger.nick))
|
|
confirm = "Added topic: " + topic
|
|
except IntegrityError:
|
|
confirm = "Error: " + topic + " is already in the database."
|
|
bot.memory['topic_lock'].release()
|
|
bot.msg(confirm)
|