Merge branch 'develop' into 'main'

added update_presence

See merge request DasMoorhuhn/tux-discord-bot!7
This commit is contained in:
DasMoorhuhn 2024-02-20 22:34:45 +00:00
commit fb52e0d842
3 changed files with 33 additions and 14 deletions

View File

@ -10,7 +10,7 @@ from discord import Interaction
from client import Client
import secret_handler
bot_info = {'version': '1.1.3', 'date': '20.02.2024'}
bot_info = {'version': '1.1.4', 'date': '20.02.2024'}
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
@ -26,35 +26,34 @@ logger.addHandler(handler)
bot = Client(intents=discord.Intents.all(), command_prefix="!", log=logger, bot_info=bot_info)
async def load_cogs():
for filename in os.listdir("./cogs"):
if filename.endswith("py") and filename != "__init__.py":
await bot.load_extension(f"cogs.{filename[:-3]}")
@bot.event
async def on_guild_join(guild: Guild):
logger.info("Joined guild")
logger.info(f"Guilds: {len(bot.guilds)}")
await bot.update_presence()
@bot.event
async def on_guild_remove(guild: Guild):
logger.info("Left guild")
logger.info(f"Guilds: {len(bot.guilds)}")
await bot.update_presence()
@bot.event
async def on_ready():
logger.info(f"Logged in as: {bot.user.name} with ID {bot.user.id}")
await load_cogs()
await bot.load_cogs()
await asyncio.sleep(1)
synced = await bot.tree.sync()
logger.info(f"Synced {len(synced)} command(s)")
await bot.update_presence()
@bot.event
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)
await interaction.response.send_message(content="You need the rights, to use this command", ephemeral=True)
else:
raise error

View File

@ -1,14 +1,19 @@
import os
import logging
from discord import Intents
from discord import Status
from discord import Game
from discord.ext import commands
import addons
class Client(commands.Bot):
"""Custom bot class"""
def __init__(self, command_prefix, *, intents: Intents, log, bot_info):
def __init__(self, command_prefix, *, intents: Intents, log:logging, bot_info:dict):
super().__init__(command_prefix, intents=intents)
self.log = log
self.addons = addons
self.log:logging = log
self.addons:addons = addons
self.start_time:float = self.addons.DateTimeHelper.get_unix_time()
self.version:str = bot_info['version']
self.date:str = bot_info['date']
@ -16,7 +21,21 @@ class Client(commands.Bot):
async def startup(self):
await self.wait_until_ready()
def get_uptime(self):
async def count_guilds(self):
"""Count the number of guilds, where the bot is joined"""
return len(self.guilds)
async def update_presence(self):
"""Update the presence"""
await self.change_presence(activity=Game(name=f"v{self.version} | {await self.count_guilds()}"), status=Status.online)
self.log.info("Updated presence")
async def get_uptime(self):
"""Returns the uptime in seconds"""
time_now = self.addons.DateTimeHelper.get_unix_time()
return int(round(time_now - self.start_time, 0))
async def load_cogs(self):
for filename in os.listdir("./cogs"):
if filename.endswith("py") and filename != "__init__.py":
await self.load_extension(f"cogs.{filename[:-3]}")

View File

@ -43,8 +43,9 @@ class HelpCommands(Cog):
self.log.info("Command: info")
bot_string = ""
bot_string += f"Uptime : {self.bot.get_uptime()}s\n"
bot_string += f"Uptime : {await self.bot.get_uptime()}s\n"
bot_string += f"Version : {self.bot.version} from {self.bot.date}\n"
bot_string += f"On Servers: {await self.bot.count_guilds()}\n"
bot_string += f"Developer : dasmoorhuhn\n"
bot_string += f"Sourcecode: [Gitlab](https://gitlab.com/DasMoorhuhn/tux-discord-bot)\n"
bot_string += f"Privacy : [Read about privacy](https://gitlab.com/DasMoorhuhn/tux-discord-bot/-/blob/main/README.md?ref_type=heads#privacy)"