1
0
Fork 0
quotes-bot-python/bot.py

162 lines
5.4 KiB
Python
Raw Normal View History

2025-01-31 18:12:38 -06:00
import simplematrixbotlib as botLibrary
2025-01-31 18:20:14 -06:00
import json
2025-01-31 19:06:34 -06:00
import fetch
2025-02-02 06:31:39 +00:00
import time
2025-01-31 18:12:38 -06:00
2025-02-01 17:25:24 -06:00
version = "0.3.0"
2025-01-31 18:20:14 -06:00
2025-01-31 20:35:55 -06:00
print("Imag/quotes bot version " + version)
print("Loading config…")
try:
with open('config.json', 'r') as f:
config = json.load(f)
print("Loaded config.")
except FileNotFoundError:
print("Config file not found. Did you read the README?")
except:
print("Something bad and unforseen happened, please report the bug to @n:everypizza.im")
2025-01-31 20:54:58 -06:00
if config['bot']['debugMode'] == True:
debugMode = True
else:
debugMode = False
2025-01-31 20:35:55 -06:00
print("Logging in…")
2025-01-31 18:38:04 -06:00
credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password'])
2025-02-01 17:11:21 -06:00
admin = config['bot']['admin']
2025-01-31 18:12:38 -06:00
bot = botLibrary.Bot(credentials)
PREFIX = "!q"
2025-01-31 20:35:55 -06:00
print("Logged in.")
print("Ready!")
2025-01-31 18:12:38 -06:00
@bot.listener.on_message_event
async def help_message(room, message):
2025-01-31 18:29:15 -06:00
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
2025-01-31 18:12:38 -06:00
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("help")):
return
2025-01-31 20:54:58 -06:00
if debugMode == False:
2025-02-02 00:13:27 -06:00
message = (f"""Help
2025-02-02 00:07:53 -06:00
============================
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
{PREFIX} help - show this message
{PREFIX} get - fetch a image from the defined instance
{PREFIX} source - send a link to the source code
{PREFIX} version - show the bot version
2025-02-02 00:13:27 -06:00
{PREFIX} die - kills the bot (only admins can do this!)""")
2025-01-31 20:54:58 -06:00
else:
2025-02-02 00:13:27 -06:00
message = (f"""Help
2025-02-02 00:07:53 -06:00
============================
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
{PREFIX} help - show this message
{PREFIX} get - fetch a image from the defined instance
{PREFIX} source - send a link to the source code
{PREFIX} version - show the bot version
{PREFIX} die - kills the bot (only admins can do this!)
---
2025-02-02 00:13:27 -06:00
NOTE: Debug mode is on. Output will be more verbose.""")
2025-01-31 18:12:38 -06:00
await bot.api.send_text_message(room.room_id, message)
2025-01-31 18:18:21 -06:00
@bot.listener.on_message_event
async def make_choice(room, message):
2025-01-31 18:29:15 -06:00
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
2025-01-31 18:18:21 -06:00
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("get")):
return
temp = True
if not match.args():
temp = False
else:
id = match.args()
2025-02-02 02:09:48 -06:00
try:
id = int(id[1])
except ValueError:
youTriedMessage = ("Blunt tried :skull:")
await bot.api.send_text_message(room.room_id, youTriedMessage)
2025-01-31 20:17:35 -06:00
quoteImage = fetch.fetchQuote(id)
2025-01-31 21:09:33 -06:00
quoteData = fetch.fetchQuoteData(id)
2025-01-31 20:54:58 -06:00
2025-02-01 16:48:31 -06:00
def formatData(data):
2025-02-02 00:11:49 -06:00
if data is not None:
2025-02-02 06:31:39 +00:00
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(data['created']))
2025-02-02 00:11:49 -06:00
dataToFormat = data
2025-02-02 06:31:39 +00:00
created = timestamp
2025-02-02 00:11:49 -06:00
description = str(data['desc'])
imageid = str(data['iid'])
ocr = str(data['ocr'])
score = str(data['score'])
return created, description, imageid, ocr, score
else:
dataToFormat = data
created = "Error"
description = "Error"
imageid = "Error"
ocr = "Error"
score = "Error"
return created, description, imageid, ocr, score
2025-02-01 16:48:31 -06:00
2025-01-31 20:54:58 -06:00
if debugMode == False:
2025-02-01 16:48:31 -06:00
created, description, imageid, ocr, score = formatData(quoteData)
2025-02-02 00:13:27 -06:00
message1 = (f"""Created: {created}
2025-02-02 00:07:53 -06:00
Description: {description}
Image ID: {imageid}
OCR (Tesseract): {ocr}
2025-02-02 00:13:27 -06:00
Rating: {score}""")
2025-01-31 20:54:58 -06:00
else:
2025-02-01 16:48:31 -06:00
created, description, imageid, ocr, score = formatData(quoteData)
2025-02-02 00:13:27 -06:00
message1 = (f"""Created: {created}
2025-02-02 00:07:53 -06:00
Description: {description}
Image ID: {imageid}
OCR (Tesseract):
> {ocr}
2025-02-02 00:13:27 -06:00
Rating: {score}""")
message2 = (f"""Command recived (DEBUG).
(We're supposed to fetch quote number {id} now.)""")
2025-01-31 18:18:21 -06:00
2025-01-31 21:09:33 -06:00
if debugMode == True:
await bot.api.send_text_message(room.room_id, message2)
await bot.api.send_text_message(room.room_id, message1)
else:
await bot.api.send_text_message(room.room_id, message1)
2025-01-31 20:17:35 -06:00
imageMessage = (quoteImage)
2025-01-31 20:18:51 -06:00
await bot.api.send_image_message(room.room_id, imageMessage) # https://quotes.everypizza.im/image/178
2025-01-31 18:29:15 -06:00
2025-01-31 19:35:15 -06:00
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
2025-01-31 19:47:23 -06:00
and match.command("source")):
2025-01-31 19:35:15 -06:00
return
2025-02-02 00:13:27 -06:00
message = (f"""quotes-bot-python by Nyx Tutt (@n:everypizza.im)
https://git.everypizza.im/n/quotes-bot-python/""")
2025-01-31 19:35:15 -06:00
await bot.api.send_text_message(room.room_id, message)
2025-01-31 20:35:55 -06:00
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
if not (match.is_not_from_this_bot() and match.prefix()
and match.command("version")):
return
2025-02-02 00:13:27 -06:00
message = (f"""quotes-bot-python version {version}""")
2025-01-31 20:35:55 -06:00
await bot.api.send_text_message(room.room_id, message)
2025-02-01 17:11:21 -06:00
@bot.listener.on_message_event
async def help_message(room, message):
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
2025-02-02 07:32:06 +00:00
if match.is_not_from_this_bot() and match.prefix() and match.command("die"):
if match.is_from_userid(admin):
await bot.api.send_text_message(room.room_id, "Ok")
exit(0)
else:
await bot.api.send_text_message(room.room_id, "You don't have permissions for that.")
2025-02-01 17:11:21 -06:00
2025-01-31 18:29:15 -06:00
bot.run()