fulvia/modules/topic.py

63 lines
1.6 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/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 Daily reminder to kill all cia niggers on site.')
def addTopic(bot, trigger):
"""
Adds the specified topic to the topic database.
"""
2020-01-07 18:58:19 -05:00
if len(trigger.args) < 2:
2018-05-25 15:21:18 -04:00
return bot.msg("Please be providing a topic sir.")
2020-01-07 18:58:19 -05:00
topic = trigger.args[1]
2018-03-16 03:13:43 -04:00
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()
2018-05-25 15:21:18 -04:00
bot.msg(confirm)