From 299351463ce9fc6578e27e07aa702ffa84ff54fc Mon Sep 17 00:00:00 2001 From: TheOnlyMace <0815cracky@gmail.com> Date: Tue, 21 Jul 2026 22:45:50 +0200 Subject: [PATCH] Refactor welcome message handling in the Autorole cog to enhance user experience. Implement dynamic embed creation for guild join events, including updated branding and links to the dashboard and support server. Improve error handling for direct messages and audit log access. --- bot/cogs/events/auto.py | 115 ++++++++++++++++++++++++++++-------- bot/cogs/events/on_guild.py | 32 ++++++---- 2 files changed, 113 insertions(+), 34 deletions(-) diff --git a/bot/cogs/events/auto.py b/bot/cogs/events/auto.py index e358231..89c7c01 100644 --- a/bot/cogs/events/auto.py +++ b/bot/cogs/events/auto.py @@ -12,40 +12,107 @@ # ╚══════════════════════════════════════════════════════════════════╝ import discord -from utils.emoji import ARROWRED, ZMODULE -from discord.utils import * from core import Axiom, Cog -from utils.Tools import * -from utils.config import BotName, serverLink +from utils.config import BotName, BRAND_NAME, DISCORD_CLIENT_ID, serverLink from discord.ext import commands from discord.ui import Button, View +# HexaHost brand accent (matches dashboard primary/accent) +EMBED_COLOR = 0xA348FF +DASHBOARD_URL = "https://bot.hexahost.de" + + class Autorole(Cog): def __init__(self, bot: Axiom): - self.bot = bot + self.bot = bot + def _invite_url(self) -> str | None: + client_id = DISCORD_CLIENT_ID or (str(self.bot.user.id) if self.bot.user else "") + if not client_id: + return None + return ( + f"https://discord.com/oauth2/authorize?client_id={client_id}" + f"&permissions=8&scope=bot%20applications.commands" + ) + + def _welcome_embed(self, guild: discord.Guild, adder: discord.abc.User) -> discord.Embed: + bot_avatar = guild.me.display_avatar.url if guild.me else ( + self.bot.user.display_avatar.url if self.bot.user else None + ) + + embed = discord.Embed( + title=f"Thanks for adding {BotName}", + description=( + f"Hey {adder.mention}, **{BotName}** is now in **{guild.name}**.\n\n" + "Here's everything you need to get started:" + ), + color=EMBED_COLOR, + ) + + if bot_avatar: + embed.set_author(name=BotName, icon_url=bot_avatar) + embed.set_thumbnail(url=bot_avatar) + + embed.add_field( + name="Prefix", + value="Default prefix is `>`\nExample: `>help`", + inline=True, + ) + embed.add_field( + name="Slash commands", + value="Type `/` in your server to browse available commands.", + inline=True, + ) + embed.add_field( + name="Dashboard", + value=f"Configure modules online at **[bot.hexahost.de]({DASHBOARD_URL})**", + inline=False, + ) + embed.add_field( + name="Need help?", + value=f"Join our **[Support Server]({serverLink})** for guides, FAQ and assistance.", + inline=False, + ) + + embed.set_footer(text=f"Powered by HexaHost · {BRAND_NAME}") + return embed + + def _welcome_view(self) -> View: + view = View() + view.add_item( + Button(label="Open Dashboard", style=discord.ButtonStyle.link, url=DASHBOARD_URL) + ) + view.add_item( + Button(label="Support Server", style=discord.ButtonStyle.link, url=serverLink) + ) + invite = self._invite_url() + if invite: + view.add_item( + Button(label="Add to another server", style=discord.ButtonStyle.link, url=invite) + ) + return view @commands.Cog.listener(name="on_guild_join") async def send_msg_to_adder(self, guild: discord.Guild): - async for entry in guild.audit_logs(limit=3): - if entry.action == discord.AuditLogAction.bot_add: - embed = discord.Embed( - description=f"{ZMODULE} **Thanks for adding me.**\n\n{ARROWRED} My default prefix is `>`\n{ARROWRED}> Use the `>help` command to see a list of commands\n{ARROWRED} For detailed guides, FAQ and information, visit our **[Support Server](https://discord.gg/hexahost)**", - color=0xFF0000 - ) - embed.set_thumbnail(url=entry.user.avatar.url if entry.user.avatar else entry.user.default_avatar.url) - embed.set_author(name=f"{guild.name}", icon_url=guild.me.display_avatar.url) - - website_button = Button(label='Website', style=discord.ButtonStyle.link, url='https://.vercel.app') - support_button = Button(label='Support', style=discord.ButtonStyle.link, url='https://discord.gg/hexahost') - vote_button = Button(label='Vote for Me', style=discord.ButtonStyle.link, url=f'https://top.gg/bot/{self.bot.user.id}/vote') - view = View() - view.add_item(support_button) - #view.add_item(website_button) - #view.add_item(vote_button) - if guild.icon: - embed.set_author(name=guild.name, icon_url=guild.icon.url) + try: + async for entry in guild.audit_logs(limit=5, action=discord.AuditLogAction.bot_add): + if entry.target is None or entry.user is None: + continue + if entry.target.id != self.bot.user.id: + continue + + embed = self._welcome_embed(guild, entry.user) + view = self._welcome_view() try: await entry.user.send(embed=embed, view=view) + except discord.Forbidden: + # User has DMs closed — ignore silently + pass except Exception as e: - print(e) + print(f"Welcome DM failed: {e}") + break + except discord.Forbidden: + # Missing View Audit Log permission + pass + except Exception as e: + print(f"Welcome DM (audit log) failed: {e}") diff --git a/bot/cogs/events/on_guild.py b/bot/cogs/events/on_guild.py index 4f70734..a7523a1 100644 --- a/bot/cogs/events/on_guild.py +++ b/bot/cogs/events/on_guild.py @@ -99,25 +99,37 @@ Threads : {len(guild.threads)} await guild.chunk() embed = discord.Embed( - description=f"{ARROWRED} Prefix For This Server is `>`\n{ARROWRED} Get Started with `>help`\n{ARROWRED} For detailed guides, FAQ & information, visit our **[Support Server](https://discord.gg/hexahost)**", - color=0xFF0000, + title=f"{BRAND_NAME} joined the server", + description=( + f"**{BRAND_NAME}** is ready to use.\n\n" + f"▸ Default prefix: `>`\n" + f"▸ Get started with `>help` or type `/`\n" + f"▸ Dashboard: **[bot.hexahost.de](https://bot.hexahost.de)**\n" + f"▸ Support: **[discord.gg/hexahost](https://discord.gg/hexahost)**" + ), + color=0xA348FF, ) embed.set_author( - name="Thanks for adding me!", icon_url=guild.me.display_avatar.url + name=BRAND_NAME, + icon_url=guild.me.display_avatar.url if guild.me else None, ) - embed.set_footer( - text=f"Powered by {BRAND_NAME}™", - ) - if guild.icon: - embed.set_thumbnail(url=guild.icon.url) + embed.set_footer(text=f"Powered by HexaHost · {BRAND_NAME}") + if guild.me: + embed.set_thumbnail(url=guild.me.display_avatar.url) support = Button( - label="Support", + label="Support Server", style=discord.ButtonStyle.link, - url=f"https://discord.gg/hexahost", + url="https://discord.gg/hexahost", + ) + dashboard = Button( + label="Open Dashboard", + style=discord.ButtonStyle.link, + url="https://bot.hexahost.de", ) view = View() + view.add_item(dashboard) view.add_item(support) channel = discord.utils.get(guild.text_channels, name="general") if not channel: