#!/usr/bin/env python3 import asyncio import os import sys import pytz from discord import Intents from discord.ext import commands from discord_webhook import DiscordEmbed, DiscordWebhook import commands as botcommands mqtt_host = os.getenv("MQTT_HOST") if not mqtt_host: print("MQTT_HOST unset") sys.exit(1) token = os.getenv("DISCORD_TOKEN") if not token: print("DISCORD_TOKEN unset") sys.exit(1) webhook_url = os.getenv("DISCORD_WEBHOOK_URL") if not webhook_url: print("DISCORD_WEBHOOK_URL unset") sys.exit(1) timezone = pytz.timezone("Europe/Amsterdam") intents = Intents.default() intents.message_content = True intents.members = True bot = commands.Bot(command_prefix="!", description="Bitlair Bot", intents=intents) botcommands.setup(bot, mqtt_host) @bot.event async def on_ready(): print(f"Logged in as {bot.user} (ID: {bot.user.id})") async def event_task(): async for message in botcommands.run_events(mqtt_host): webhook = DiscordWebhook(url=webhook_url, rate_limit_retry=True) if type(message) is str: webhook.content = message elif type(message) is DiscordEmbed: webhook.add_embed(message) else: print(f"invalid message type: {str(message)}") continue webhook.execute() async def main(): t1 = asyncio.create_task(bot.start(token)) t2 = asyncio.create_task(event_task()) await asyncio.gather(t1, t2) if __name__ == "__main__": asyncio.run(main())