import time as t import asyncio import logging import datetime import secret_handler from addons import ProgrammerExcuses from addons import XKCD from models.xkcdComic import Comic import discord from discord import Status from discord import app_commands from discord import Interaction from discord import Embed from discord import Guild from discord import Color from discord.activity import Game botVersion = "1.0.24" botDate = "16.02.2024" secret = secret_handler.get_bot_token() # Init Logger logger = logging.getLogger('discord') logger.setLevel(logging.DEBUG) handler = logging.FileHandler(filename='data/log/discord.log', encoding='utf-8', mode='a') handler.setFormatter(logging.Formatter('%(asctime)s|%(levelname)s|%(name)s|:%(message)s')) logger.addHandler(handler) # Init Addons programmer_excuses = ProgrammerExcuses() # redditProgrammerHumor = RedditProgrammerHumor() xkcd = XKCD() class Client(discord.Client): async def startup(self): await self.wait_until_ready() def get_date_time(): date_now = datetime.datetime.now() date_now = str(date_now).split(" ") unix = t.time() time = date_now[1] time = time.split(".") time = time[0] date_now = date_now[0] return date_now, time, unix start_time = get_date_time()[2] bot = Client(intents=discord.Intents.all()) tree = app_commands.CommandTree(client=bot) async def change_presence(interval_in_seconds=120): while True: await bot.change_presence(activity=Game(name="with penguins"), status=Status.online) await asyncio.sleep(interval_in_seconds) count_guilds = 0 async for guild in bot.fetch_guilds(): count_guilds += 1 await bot.change_presence(activity=Game(name=f"on {count_guilds} Servers"), status=Status.online) await asyncio.sleep(interval_in_seconds) @bot.event async def on_ready(): start_time = get_date_time()[2] await bot.loop.create_task(change_presence()) logger.info(f"Logged in as: {bot.user.name} with ID {bot.user.id}") await bot.change_presence(activity=Game(name="with penguins"), status=Status.online) await tree.sync() @bot.event async def on_guild_join(guild: Guild): logger.debug("Added to Guild") await guild.system_channel.send("Hii^^") @tree.command(name='excuse', description='Get a random excuse from programmingexcuses') async def slash(interaction: Interaction): logger.debug("Command: excuse") await interaction.response.send_message(programmer_excuses.get_excuse()) @tree.command(name='get-latest-comic', description='Get latest comic from XKCD') async def slash(interaction: Interaction): logger.debug("Command: get-latest-comic") comic = xkcd.get_last_comic() embed = discord.Embed(title=comic.title, color=Color.blue(), url=f"{xkcd.url}/{comic.num}") embed.set_image(url=comic.img) await interaction.response.send_message(embed=embed) @tree.command(name='get-random-comic', description='Get a random comic from XKCD') async def slash(interaction: Interaction): logger.debug("Command: get-random-comic") comic = xkcd.get_random_comic() embed = discord.Embed(title=comic.title, color=Color.blue(), url=f"{xkcd.url}/{comic.num}") embed.set_image(url=comic.img) await interaction.response.send_message(embed=embed) # @tree.command(name='programmer-humor', description='Get a random Post from r/ProgrammerHumor') # async def slash(interaction: Interaction): # post = redditProgrammerHumor.getRandomPost() # await interaction.response.send_message("hi") @tree.command(name="info", description="get info about this bot") async def slash(interaction: Interaction): logger.debug("Command: info") time_now = get_date_time()[2] uptime = time_now - start_time bot_string = "" bot_string += f"Uptime : {int(round(uptime, 0))}s\n" bot_string += f"Version : {botVersion} from {botDate}\n" bot_string += f"Developer : dasmoorhuhn\n" bot_string += f"Sourcecode: https://gitlab.com/DasMoorhuhn/tux-discord-bot" embed = discord.Embed(title=f"Info", description="about this Bot", timestamp=datetime.datetime.utcnow(), color=Color.blue()) # embed.set_thumbnail(url=interaction.guild.icon) embed.add_field(name="Bot", value=bot_string, inline=False) await interaction.response.send_message(embed=embed) @tree.command(name="help", description="List of all Commands") async def slash(interaction: Interaction): logger.debug("Command: help") command_list_string = "" command_list_string += "`/info` : Get infos about the server and the Bot\n" command_list_string += "`/help` : Get this view\n" command_list_string += "`/get-random-comic`: Get a randowm XCCD comic\n" command_list_string += "`/get-latest-comic`: Get latest comic from XKCD\n" command_list_string += "`/excuse` : Get a random excuse from programmingexcuses" embed = discord.Embed(title=f"Help", description="List of commands", color=Color.blue()) # embed.set_thumbnail(url=interaction.guild.icon) embed.add_field(name="Commands", value=command_list_string, inline=True) await interaction.response.send_message(embed=embed) @tree.error async def on_app_command_error(interaction: Interaction, error): if isinstance(error, app_commands.MissingPermissions): await interaction.response.send_message(content="Du hast keine Adminrechte", ephemeral=True) else: raise error # Prevent Gateway Heartbeat Block try: bot.run(token=secret_handler.get_bot_token()) except Exception as err: raise err finally: logger.debug("Stopped")