Compare commits
13 commits
Author | SHA1 | Date | |
---|---|---|---|
cbfdd831a7 | |||
528e1e81a1 | |||
0d5b7cb854 | |||
96ba34f17b | |||
b283f66bb8 | |||
ee73b777ba | |||
e4df14c3b7 | |||
68e3e2020c | |||
b572834329 | |||
b685fa6e18 | |||
216c1bf4d7 | |||
8deaabf5d8 | |||
66ff8f1474 |
3 changed files with 79 additions and 56 deletions
76
bot.py
76
bot.py
|
@ -1,8 +1,9 @@
|
||||||
import simplematrixbotlib as botLibrary
|
import simplematrixbotlib as botLibrary
|
||||||
import json
|
import json
|
||||||
import fetch
|
import fetch
|
||||||
|
import time
|
||||||
|
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
|
|
||||||
print("Imag/quotes bot version " + version)
|
print("Imag/quotes bot version " + version)
|
||||||
print("Loading config…")
|
print("Loading config…")
|
||||||
|
@ -22,6 +23,7 @@ else:
|
||||||
|
|
||||||
print("Logging in…")
|
print("Logging in…")
|
||||||
credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password'])
|
credentials = botLibrary.Creds(config['matrix']['server'], config['matrix']['userid'], config['matrix']['password'])
|
||||||
|
admin = config['bot']['admin']
|
||||||
bot = botLibrary.Bot(credentials)
|
bot = botLibrary.Bot(credentials)
|
||||||
PREFIX = "!q"
|
PREFIX = "!q"
|
||||||
print("Logged in.")
|
print("Logged in.")
|
||||||
|
@ -33,29 +35,27 @@ async def help_message(room, message):
|
||||||
and match.command("help")):
|
and match.command("help")):
|
||||||
return
|
return
|
||||||
if debugMode == False:
|
if debugMode == False:
|
||||||
message = (f"""
|
message = (f"""Help
|
||||||
Help
|
|
||||||
============================
|
============================
|
||||||
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
|
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
|
||||||
{PREFIX} help - show this message
|
{PREFIX} help - show this message
|
||||||
{PREFIX} get - fetch a image from the defined instance
|
{PREFIX} get - fetch a image from the defined instance
|
||||||
{PREFIX} source - send a link to the source code
|
{PREFIX} source - send a link to the source code
|
||||||
{PREFIX} version - show the bot version
|
{PREFIX} version - show the bot version
|
||||||
""")
|
{PREFIX} die - kills the bot (only admins can do this!)""")
|
||||||
else:
|
else:
|
||||||
message = (f"""
|
message = (f"""Help
|
||||||
Help
|
|
||||||
============================
|
============================
|
||||||
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
|
A work-in-progress Python rewrite of the original quotes bot, aiming for a mostly complete remake.
|
||||||
{PREFIX} help - show this message
|
{PREFIX} help - show this message
|
||||||
{PREFIX} get - fetch a image from the defined instance
|
{PREFIX} get - fetch a image from the defined instance
|
||||||
{PREFIX} source - send a link to the source code
|
{PREFIX} source - send a link to the source code
|
||||||
{PREFIX} version - show the bot version
|
{PREFIX} version - show the bot version
|
||||||
|
{PREFIX} die - kills the bot (only admins can do this!)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
NOTE: Debug mode is on. Output will be more verbose.
|
NOTE: Debug mode is on. Output will be more verbose.""")
|
||||||
""")
|
|
||||||
|
|
||||||
|
|
||||||
await bot.api.send_text_message(room.room_id, message)
|
await bot.api.send_text_message(room.room_id, message)
|
||||||
|
@ -71,41 +71,51 @@ async def make_choice(room, message):
|
||||||
temp = False
|
temp = False
|
||||||
else:
|
else:
|
||||||
id = match.args()
|
id = match.args()
|
||||||
id = str(id[1])
|
try:
|
||||||
|
id = int(id[1])
|
||||||
|
except ValueError:
|
||||||
|
youTriedMessage = ("Blunt tried :skull:")
|
||||||
|
await bot.api.send_text_message(room.room_id, youTriedMessage)
|
||||||
|
|
||||||
quoteImage = fetch.fetchQuote(id)
|
quoteImage = fetch.fetchQuote(id)
|
||||||
quoteData = fetch.fetchQuoteData(id)
|
quoteData = fetch.fetchQuoteData(id)
|
||||||
|
|
||||||
def formatData(data):
|
def formatData(data):
|
||||||
|
if data is not None:
|
||||||
|
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(data['created']))
|
||||||
dataToFormat = data
|
dataToFormat = data
|
||||||
created = str(data['created'])
|
created = timestamp
|
||||||
description = str(data['desc'])
|
description = str(data['desc'])
|
||||||
imageid = str(data['iid'])
|
imageid = str(data['iid'])
|
||||||
ocr = str(data['ocr'])
|
ocr = str(data['ocr'])
|
||||||
score = str(data['score'])
|
score = str(data['score'])
|
||||||
return created, description, imageid, ocr, 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
|
||||||
|
|
||||||
if debugMode == False:
|
if debugMode == False:
|
||||||
created, description, imageid, ocr, score = formatData(quoteData)
|
created, description, imageid, ocr, score = formatData(quoteData)
|
||||||
message1 = (f"""
|
message1 = (f"""Created: {created}
|
||||||
Created: {created}
|
|
||||||
Description: {description}
|
Description: {description}
|
||||||
Image ID: {imageid}
|
Image ID: {imageid}
|
||||||
OCR (Tesseract): {ocr}
|
OCR (Tesseract): {ocr}
|
||||||
Rating: {score}
|
Rating: {score}""")
|
||||||
""")
|
|
||||||
else:
|
else:
|
||||||
created, description, imageid, ocr, score = formatData(quoteData)
|
created, description, imageid, ocr, score = formatData(quoteData)
|
||||||
message1 = (f"""
|
message1 = (f"""Created: {created}
|
||||||
Created: {created}
|
|
||||||
Description: {description}
|
Description: {description}
|
||||||
Image ID: {imageid}
|
Image ID: {imageid}
|
||||||
OCR (Tesseract): {ocr}
|
OCR (Tesseract):
|
||||||
Rating: {score}
|
> {ocr}
|
||||||
""")
|
Rating: {score}""")
|
||||||
message2 = (f"""
|
message2 = (f"""Command recived (DEBUG).
|
||||||
Command recived (DEBUG).
|
(We're supposed to fetch quote number {id} now.)""")
|
||||||
(We're supposed to fetch quote number {id} now.)
|
|
||||||
""")
|
|
||||||
|
|
||||||
if debugMode == True:
|
if debugMode == True:
|
||||||
await bot.api.send_text_message(room.room_id, message2)
|
await bot.api.send_text_message(room.room_id, message2)
|
||||||
|
@ -122,10 +132,8 @@ async def help_message(room, message):
|
||||||
and match.command("source")):
|
and match.command("source")):
|
||||||
return
|
return
|
||||||
|
|
||||||
message = (f"""
|
message = (f"""quotes-bot-python by Nyx Tutt (@n:everypizza.im)
|
||||||
quotes-bot-python by Nyx Tutt (@n:everypizza.im)
|
https://git.everypizza.im/n/quotes-bot-python/""")
|
||||||
https://git.everypizza.im/n/quotes-bot-python/
|
|
||||||
""")
|
|
||||||
|
|
||||||
await bot.api.send_text_message(room.room_id, message)
|
await bot.api.send_text_message(room.room_id, message)
|
||||||
|
|
||||||
|
@ -136,10 +144,18 @@ async def help_message(room, message):
|
||||||
and match.command("version")):
|
and match.command("version")):
|
||||||
return
|
return
|
||||||
|
|
||||||
message = (f"""
|
message = (f"""quotes-bot-python version {version}""")
|
||||||
quotes-bot-python version {version}
|
|
||||||
""")
|
|
||||||
|
|
||||||
await bot.api.send_text_message(room.room_id, message)
|
await bot.api.send_text_message(room.room_id, message)
|
||||||
|
|
||||||
|
@bot.listener.on_message_event
|
||||||
|
async def help_message(room, message):
|
||||||
|
match = botLibrary.MessageMatch(room, message, bot, PREFIX)
|
||||||
|
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.")
|
||||||
|
|
||||||
bot.run()
|
bot.run()
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"server": "https://quotes.example.com"
|
"server": "https://quotes.example.com"
|
||||||
},
|
},
|
||||||
"bot": {
|
"bot": {
|
||||||
"debugMode": true
|
"debugMode": true,
|
||||||
|
"admin": "@alice:example.com"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
8
fetch.py
8
fetch.py
|
@ -30,6 +30,12 @@ def fetchQuoteData(quoteId):
|
||||||
print(quoteData.json())
|
print(quoteData.json())
|
||||||
else:
|
else:
|
||||||
print(f"An error occured while saving the file: {quoteData.status_code}")
|
print(f"An error occured while saving the file: {quoteData.status_code}")
|
||||||
return "This file probably doesn't exist."
|
return None
|
||||||
|
|
||||||
|
def fetchTopQuotes(instance):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def fetchBottomQuotes(instance):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
fetchQuoteData(178)
|
fetchQuoteData(178)
|
||||||
|
|
Loading…
Add table
Reference in a new issue