You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.5 KiB
94 lines
2.5 KiB
#! /usr/bin/env python3
|
|
"""
|
|
Code for schneiderbot
|
|
"""
|
|
import os
|
|
import datetime
|
|
import urllib.request
|
|
import json
|
|
import logging
|
|
import random
|
|
|
|
import discord
|
|
from discord.ext import commands
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
DESCRIPTION = '''Schneiderbot, bald mit tollen Funktionen'''
|
|
BOT = commands.Bot(command_prefix='!', description=DESCRIPTION)
|
|
MENSA_URL = {
|
|
"zentral": "zentralmensa",
|
|
"nord": "nordmensa",
|
|
"turm": "turmmensa"
|
|
}
|
|
MENSA_NAME = {
|
|
"zentral": "Zentralmensa",
|
|
"nord": "Nordmensa",
|
|
"turm": "Turmmensa"
|
|
}
|
|
|
|
with open("stoll.txt", "r") as tmp_file:
|
|
STOLL = tmp_file.readlines()
|
|
|
|
@BOT.event
|
|
async def on_ready():
|
|
""" On ready """
|
|
print('Logged in as')
|
|
print(BOT.user.name)
|
|
print(BOT.user.id)
|
|
print('------')
|
|
|
|
@BOT.command()
|
|
async def hello():
|
|
"""Was ist das hier für eins Bot?"""
|
|
await BOT.say("""Hallo, ich bin ein Bot.
|
|
Ich liefere tolle Informationen über die Mensa. Die Infos kommen von
|
|
https://mensa.schneider-hosting.de
|
|
Mhmm, lecker. Guten Appetit!""")
|
|
|
|
@BOT.command()
|
|
async def mensa(which="zentral"):
|
|
"""Gibt leckere, äh, nützliche Infos über die Mensa aus.
|
|
Standardmäßig für die Zentralmensa, aber probier auch mal nord oder turm aus."""
|
|
|
|
if which not in MENSA_NAME:
|
|
await BOT.say("Diese Mensa gibt es nicht! RTFM und versuchs nochmal.")
|
|
return
|
|
|
|
today = datetime.datetime.now().date().weekday() + 1
|
|
if datetime.datetime.now().time() > datetime.time(hour=16):
|
|
# Es ist zu spät am Tag, zeig das essen für morgen an
|
|
today += 1
|
|
|
|
with urllib.request.urlopen("https://mensa.schneider-hosting.de/static/%s.%d.json" % (MENSA_URL[which], today)) as url:
|
|
data = json.loads(url.read().decode())
|
|
|
|
await BOT.say("Das Essen für %s in der %s" % (data["date"], MENSA_NAME[which]))
|
|
|
|
message = ""
|
|
for meal in data["meals"]:
|
|
meal_line = "**%s**\n" % meal["category"]
|
|
meal_line += meal["title"].strip() + "\n"
|
|
|
|
# Discord only allows up to 2000 chars per message
|
|
if len(message) + len(meal_line) > 2000:
|
|
await BOT.say(message)
|
|
message = meal_line
|
|
else:
|
|
message += meal_line
|
|
|
|
await BOT.say(message)
|
|
|
|
@BOT.command()
|
|
async def magie():
|
|
"""MAGIEEEEEE"""
|
|
rand = random.SystemRandom()
|
|
await BOT.say(STOLL[rand.randrange(0, len(STOLL), 1)])
|
|
|
|
def main():
|
|
""" entry point """
|
|
token = open("token").read()
|
|
BOT.run(token.strip())
|
|
|
|
if __name__ == '__main__':
|
|
main()
|