summaryrefslogtreecommitdiff
path: root/post-poll.py
diff options
context:
space:
mode:
authorBenji Dial <benji@benjidial.net>2024-09-05 21:16:43 -0400
committerBenji Dial <benji@benjidial.net>2024-09-05 21:16:43 -0400
commit87d07ac5b9a4529199cb3f807e4ceff982c588c2 (patch)
tree6c7b60df12bec73b65c893ad99308cf1f18e07b1 /post-poll.py
parentd70bd2d83d68f6d51606b2101c318f6f0ff89c85 (diff)
downloadbracket-bot-87d07ac5b9a4529199cb3f807e4ceff982c588c2.tar.gz
new new version
Diffstat (limited to 'post-poll.py')
-rw-r--r--post-poll.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/post-poll.py b/post-poll.py
new file mode 100644
index 0000000..8a5bc98
--- /dev/null
+++ b/post-poll.py
@@ -0,0 +1,79 @@
+from os import environ
+import argparse
+import discord
+import sqlite3
+import time
+
+ap = argparse.ArgumentParser()
+ap.add_argument('database', help='the file with the database')
+ap.add_argument('channel', help='the discord id of the channel to post to')
+ap.add_argument('hours', help='the number of hours to have the poll open for')
+
+a = ap.parse_args()
+
+hours = int(a.hours)
+
+if 'TOKEN' not in environ:
+ print('please set an environment variable TOKEN with the bot token')
+ exit(1)
+
+token = environ['TOKEN']
+
+dbcon = sqlite3.connect(a.database)
+
+cur = dbcon.cursor()
+
+last_number = cur.execute('''
+ SELECT poll_number FROM polls
+ ORDER BY poll_number DESC
+ LIMIT 1
+''').fetchone()
+
+poll_number = last_number[0] + 1 if last_number else 1
+
+rows = cur.execute('''
+ SELECT name, emoji FROM entries
+ WHERE status == 0
+ ORDER BY last_poll_number, sort_number
+ LIMIT 2
+''').fetchall()
+
+if len(rows) < 2:
+ print('there aren\'t two entries available')
+ exit(0)
+
+def media(name, emoji):
+ emoji_obj = {'id': emoji} if emoji.isascii() and emoji.isdigit() else {'name': emoji}
+ return {'text': name, 'emoji': emoji_obj}
+
+result = discord.post(discord.session(token), f'/channels/{a.channel}/messages', {
+ 'poll': {
+ 'question': {'text': f'Poll #{poll_number}'},
+ 'answers': [{
+ 'poll_media': media(rows[0][0], rows[0][1])
+ }, {
+ 'poll_media': media(rows[1][0], rows[1][1])
+ }],
+ 'duration': hours
+ }
+})
+
+posted = int(time.time())
+expires = posted + hours * 3600
+channel_id = result['channel_id']
+message_id = result['id']
+
+cur.execute('INSERT INTO polls VALUES(?, ?, ?, ?, ?, ?, ?, NULL, NULL)',
+ (poll_number, rows[0][0], rows[1][0], posted, expires, channel_id, message_id))
+
+cur.executemany('''
+ UPDATE entries
+ SET status = 1, last_poll_number = ?
+ WHERE name = ?
+ LIMIT 1
+''', [
+ (poll_number, rows[0][0]),
+ (poll_number, rows[1][0])
+])
+
+dbcon.commit()