Splitting into smaller files
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
from telegram import Update
|
||||||
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
# /liste Handler (nur privat für Admins)
|
||||||
|
async def liste(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if update.effective_user.id not in config['admin']['ids']:
|
||||||
|
await update.message.reply_text("Epic fail, kein Zugriff für dich")
|
||||||
|
return
|
||||||
|
|
||||||
|
if event_list:
|
||||||
|
antwort = "*Aktuelle Anmeldungen:*\n\n"
|
||||||
|
for i, name in enumerate(event_list, start=1):
|
||||||
|
antwort += f"{i}. {name}\n"
|
||||||
|
antwort += f"\n Insgesamt angemeldet: *{len(event_list)}*"
|
||||||
|
await update.message.reply_text(
|
||||||
|
antwort, parse_mode="Markdown"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await update.message.reply_text("Chill, es is noch gar keiner angemeldet.")
|
||||||
|
|
||||||
|
# /reset Handler (nur privat für Admins, mit Archivierung)
|
||||||
|
async def reset(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if update.effective_user.id not in config['admin']['ids']:
|
||||||
|
await update.message.reply_text("Epic fail, Zugriff verweigert")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Nur archivieren, wenn es überhaupt etwas gibt
|
||||||
|
if event_list:
|
||||||
|
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
|
backup_file = f"event_list_{timestamp}.json"
|
||||||
|
try:
|
||||||
|
shutil.copy(list_file, backup_file)
|
||||||
|
await update.message.reply_text(
|
||||||
|
f"Ich hab die Liste mal gespeichert, falls du sie aus Versehen resettet hast, liegt jetz unter:\n`{backup_file}`",
|
||||||
|
parse_mode="Markdown"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
await update.message.reply_text(f"Fehler beim Sichern: {e}")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text("Liste ist leer, nichts zu sichern")
|
||||||
|
|
||||||
|
# Liste zurücksetzen (neue, leere Liste)
|
||||||
|
event_list.clear()
|
||||||
|
save_list()
|
||||||
|
await update.message.reply_text("WOOOSH! Und die Liste is wieder leer.")
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
from telegram import Update
|
||||||
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
# /anmeldung Handler
|
||||||
|
async def anmeldung(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if not context.args:
|
||||||
|
await update.message.reply_text("Bitte Namen angeben: /anmeldung Name")
|
||||||
|
return
|
||||||
|
name = " ".join(context.args)
|
||||||
|
if name in event_list:
|
||||||
|
await update.message.reply_text(f"{name}, du bist doch schon angemeldet.")
|
||||||
|
else:
|
||||||
|
event_list.append(name)
|
||||||
|
save_list()
|
||||||
|
await update.message.reply_text(f"{name} will unbedingt auch dabei sein!")
|
||||||
|
|
||||||
|
# /abmeldung Handler
|
||||||
|
async def abmeldung(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
if not context.args:
|
||||||
|
await update.message.reply_text("Bitte Namen angeben: /abmeldung Name")
|
||||||
|
return
|
||||||
|
name = " ".join(context.args)
|
||||||
|
if name in event_list:
|
||||||
|
event_list.remove(name)
|
||||||
|
save_list()
|
||||||
|
await update.message.reply_text(f"{name} kann nich mehr oder hat kein Bock auf uns.")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text(f"{name}, du warst eh nich auf der Liste.")
|
||||||
|
|
||||||
|
# /hilfe Handler
|
||||||
|
async def hilfe(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
text = (
|
||||||
|
"Dude, ich erklär dir mal wie das hier läuft:\n\n"
|
||||||
|
"In der Gruppe:\n"
|
||||||
|
"/anmeldung Name - zum Anmelden\n"
|
||||||
|
"/abmeldung Name - zum Abmelden\n"
|
||||||
|
"/status - check wie viele Fussel dabei sind\n\n"
|
||||||
|
)
|
||||||
|
await update.message.reply_text(text)
|
||||||
|
|
||||||
|
# /status Handler (für alle)
|
||||||
|
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
count = len(event_list)
|
||||||
|
if count == 0:
|
||||||
|
text = "Is noch keiner eingecheckt..."
|
||||||
|
elif count == 1:
|
||||||
|
text = "Es ist aktuell *1 Fussel* dabei!"
|
||||||
|
else:
|
||||||
|
text = f"Es sind aktuell *{count} Fussel* dabei!"
|
||||||
|
await update.message.reply_text(text, parse_mode="Markdown")
|
||||||
|
|
||||||
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
await hilfe(update, context)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
from telegram import Update
|
||||||
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
await update.message.reply_text("nope")
|
||||||
|
return
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from telegram import Update
|
||||||
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from telegram import Update
|
||||||
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
+34
-109
@@ -1,132 +1,43 @@
|
|||||||
|
# [ IMPORTS ] #
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
||||||
import configparser
|
import configparser
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
# other files in here
|
||||||
|
from errors import *
|
||||||
|
from commands_admin import *
|
||||||
|
from commands_user import *
|
||||||
|
from handler_participant_lists import *
|
||||||
|
from log import *
|
||||||
|
|
||||||
|
#############################################
|
||||||
|
# [ CONFIG FILES ] #
|
||||||
|
|
||||||
# Konfiguration
|
# Konfiguration
|
||||||
list_file = "event_list.json"
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read('telegram_bot_token.cfg')
|
config.read('telegram_bot_token.cfg')
|
||||||
config.read('telegram_bot_config.cfg')
|
config.read('telegram_bot_config.cfg')
|
||||||
print("Configs loaded.")
|
print("Configs loaded.")
|
||||||
|
|
||||||
# Lade die Liste, falls vorhanden
|
#############################################
|
||||||
if os.path.exists(list_file):
|
# [ MAIN LOOP ] #
|
||||||
with open(list_file, "r") as f:
|
|
||||||
event_list = json.load(f)
|
|
||||||
else:
|
|
||||||
event_list = []
|
|
||||||
|
|
||||||
# Hilfsfunktion zum Speichern
|
|
||||||
def save_list():
|
|
||||||
with open(list_file, "w") as f:
|
|
||||||
json.dump(event_list, f)
|
|
||||||
|
|
||||||
# /anmeldung Handler
|
|
||||||
async def anmeldung(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
if not context.args:
|
|
||||||
await update.message.reply_text("Bitte Namen angeben: /anmeldung Name")
|
|
||||||
return
|
|
||||||
name = " ".join(context.args)
|
|
||||||
if name in event_list:
|
|
||||||
await update.message.reply_text(f"{name}, du bist doch schon angemeldet.")
|
|
||||||
else:
|
|
||||||
event_list.append(name)
|
|
||||||
save_list()
|
|
||||||
await update.message.reply_text(f"{name} will unbedingt auch dabei sein!")
|
|
||||||
|
|
||||||
# /abmeldung Handler
|
|
||||||
async def abmeldung(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
if not context.args:
|
|
||||||
await update.message.reply_text("Bitte Namen angeben: /abmeldung Name")
|
|
||||||
return
|
|
||||||
name = " ".join(context.args)
|
|
||||||
if name in event_list:
|
|
||||||
event_list.remove(name)
|
|
||||||
save_list()
|
|
||||||
await update.message.reply_text(f"{name} kann nich mehr oder hat kein Bock auf uns.")
|
|
||||||
else:
|
|
||||||
await update.message.reply_text(f"{name}, du warst eh nich auf der Liste.")
|
|
||||||
|
|
||||||
# /liste Handler (nur privat für Admins)
|
|
||||||
async def liste(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
if update.effective_user.id not in config['admin']['ids']:
|
|
||||||
await update.message.reply_text("Epic fail, kein Zugriff für dich")
|
|
||||||
return
|
|
||||||
|
|
||||||
if event_list:
|
|
||||||
antwort = "*Aktuelle Anmeldungen:*\n\n"
|
|
||||||
for i, name in enumerate(event_list, start=1):
|
|
||||||
antwort += f"{i}. {name}\n"
|
|
||||||
antwort += f"\n Insgesamt angemeldet: *{len(event_list)}*"
|
|
||||||
await update.message.reply_text(
|
|
||||||
antwort, parse_mode="Markdown"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
await update.message.reply_text("Chill, es is noch gar keiner angemeldet.")
|
|
||||||
|
|
||||||
# /reset Handler (nur privat für Admins, mit Archivierung)
|
|
||||||
import datetime
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
async def reset(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
if update.effective_user.id not in config['admin']['ids']:
|
|
||||||
await update.message.reply_text("Epic fail, Zugriff verweigert")
|
|
||||||
return
|
|
||||||
|
|
||||||
# Nur archivieren, wenn es überhaupt etwas gibt
|
|
||||||
if event_list:
|
|
||||||
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
|
||||||
backup_file = f"event_list_{timestamp}.json"
|
|
||||||
try:
|
|
||||||
shutil.copy(list_file, backup_file)
|
|
||||||
await update.message.reply_text(
|
|
||||||
f"Ich hab die Liste mal gespeichert, falls du sie aus Versehen resettet hast, liegt jetz unter:\n`{backup_file}`",
|
|
||||||
parse_mode="Markdown"
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
await update.message.reply_text(f"Fehler beim Sichern: {e}")
|
|
||||||
else:
|
|
||||||
await update.message.reply_text("Liste ist leer, nichts zu sichern")
|
|
||||||
|
|
||||||
# Liste zurücksetzen (neue, leere Liste)
|
|
||||||
event_list.clear()
|
|
||||||
save_list()
|
|
||||||
await update.message.reply_text("WOOOSH! Und die Liste is wieder leer.")
|
|
||||||
|
|
||||||
# /hilfe Handler
|
|
||||||
async def hilfe(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
text = (
|
|
||||||
"Dude, ich erklär dir mal wie das hier läuft:\n\n"
|
|
||||||
"In der Gruppe:\n"
|
|
||||||
"/anmeldung Name - zum Anmelden\n"
|
|
||||||
"/abmeldung Name - zum Abmelden\n"
|
|
||||||
"/status - check wie viele Fussel dabei sind\n\n"
|
|
||||||
)
|
|
||||||
await update.message.reply_text(text)
|
|
||||||
|
|
||||||
# /status Handler (für alle)
|
|
||||||
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
||||||
count = len(event_list)
|
|
||||||
if count == 0:
|
|
||||||
text = "Is noch keiner eingecheckt..."
|
|
||||||
elif count == 1:
|
|
||||||
text = "Es ist aktuell *1 Fussel* dabei!"
|
|
||||||
else:
|
|
||||||
text = f"Es sind aktuell *{count} Fussel* dabei!"
|
|
||||||
await update.message.reply_text(text, parse_mode="Markdown")
|
|
||||||
|
|
||||||
# Bot starten
|
|
||||||
def main():
|
def main():
|
||||||
app = ApplicationBuilder().token(config['telegram']['bot_token']).build()
|
app = ApplicationBuilder().token(config['telegram']['bot_token']).build()
|
||||||
|
|
||||||
app.add_handler(CommandHandler("anmeldung", anmeldung))
|
# Admin commands
|
||||||
app.add_handler(CommandHandler("abmeldung", abmeldung))
|
|
||||||
app.add_handler(CommandHandler("liste", liste))
|
app.add_handler(CommandHandler("liste", liste))
|
||||||
app.add_handler(CommandHandler("reset", reset))
|
app.add_handler(CommandHandler("reset", reset))
|
||||||
|
|
||||||
|
# User commands
|
||||||
|
app.add_handler(CommandHandler("start", hilfe))
|
||||||
app.add_handler(CommandHandler("hilfe", hilfe))
|
app.add_handler(CommandHandler("hilfe", hilfe))
|
||||||
|
app.add_handler(CommandHandler("anmeldung", anmeldung))
|
||||||
|
app.add_handler(CommandHandler("abmeldung", abmeldung))
|
||||||
app.add_handler(CommandHandler("status", status))
|
app.add_handler(CommandHandler("status", status))
|
||||||
|
app.add_handler(CommandHandler("error", error))
|
||||||
|
|
||||||
print("Bot läuft...")
|
print("Bot läuft...")
|
||||||
app.run_polling()
|
app.run_polling()
|
||||||
@@ -135,3 +46,17 @@ if __name__ == "__main__":
|
|||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
######### NEEDS TO BE MOVED #####
|
||||||
|
# Lade die Liste, falls vorhanden
|
||||||
|
if os.path.exists(list_file):
|
||||||
|
with open(config['list']['default'], "r") as f:
|
||||||
|
event_list = json.load(f)
|
||||||
|
else:
|
||||||
|
event_list = []
|
||||||
|
|
||||||
|
# Hilfsfunktion zum Speichern
|
||||||
|
def save_list():
|
||||||
|
with open(config['list']['default'], "w") as f:
|
||||||
|
json.dump(event_list, f)
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ ids = [CHANGE_ME, CHANGE_ME]
|
|||||||
[chats]
|
[chats]
|
||||||
# allowed_chat_ids = [CHANGEME]
|
# allowed_chat_ids = [CHANGEME]
|
||||||
# allow_dms_if_not_in_allowed_chats = True
|
# allow_dms_if_not_in_allowed_chats = True
|
||||||
|
|
||||||
|
[list]
|
||||||
|
default = event_list.json
|
||||||
|
|||||||
Reference in New Issue
Block a user