kaybot/kaybot.py

44 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
from pleroma_bot import PleromaBot
from mastodon import Mastodon
from pathlib import Path
import qtoml as toml
import urllib.parse
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://cannibal.cafe/KayFaraday. I only run when summoned.')
@bot.command
def ping(notif, *_):
"""Replies with “pong”"""
pleroma.status_reply(notif['status'], 'Pong!')
pastas = {p.stem: p.read_text() for p in Path('pasta').glob('*.txt')}
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
@bot.command
def pasta(notif, pasta_name='', *_):
"""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:
acc = notif['status']['account']
formatted = pastas[pasta_name].format(
username=acc['username'],
instance=get_instance(acc['acct']),
)
pleroma.status_reply(notif['status'], formatted)
if __name__ == '__main__':
bot.run()