kaybot/kaybot.py

44 lines
1.2 KiB
Python
Raw Normal View History

2022-12-20 16:36:48 -08:00
#!/usr/bin/env python
from pleroma_bot import PleromaBot
from mastodon import Mastodon
2022-12-20 16:59:28 -08:00
from pathlib import Path
2022-12-20 16:36:48 -08:00
import qtoml as toml
2023-02-04 17:59:56 -08:00
import urllib.parse
2022-12-20 16:36:48 -08:00
with open('config.toml') as f:
config = toml.load(f)
pleroma = Mastodon(**config['creds'])
bot = PleromaBot(pleroma, about='I am a basic bot created by https://freak.university/@KayFaraday. I only run when summoned.')
@bot.command
def ping(notif, *_):
"""Replies with “pong”"""
pleroma.status_reply(notif['status'], 'Pong!')
2022-12-20 16:59:28 -08:00
pastas = {p.stem: p.read_text() for p in Path('pasta').glob('*.txt')}
2023-02-04 17:59:56 -08:00
home_instance = urllib.parse.urlparse(config['creds']['api_base_url']).netloc
def get_instance(fqn):
username, at, instance = fqn.partition('@')
if not at:
return home_instance
return instance
2022-12-20 16:59:28 -08:00
@bot.command
2022-12-22 13:25:36 -08:00
def pasta(notif, pasta_name='', *_):
2022-12-20 16:59:28 -08:00
"""Pulls up a copypasta. Run with no arguments to get a list of available copypastas."""
if not pasta_name:
pleroma.status_reply(notif['status'], f'Available copypasta: {", ".join(pastas)}')
else:
2023-02-04 17:59:56 -08:00
acc = notif['status']['account']
formatted = pastas[pasta_name].format(
username=acc['username'],
instance=get_instance(acc['acct']),
)
pleroma.status_reply(notif['status'], formatted)
2022-12-20 16:59:28 -08:00
2022-12-20 16:36:48 -08:00
if __name__ == '__main__':
bot.run()