Skip to content

Commit

Permalink
feat(bans): implement direct messaging on user ban in team_utils
Browse files Browse the repository at this point in the history
The update adds the functionality to send users a Direct Message (DM) upon getting banned from the server. This involves checking if the user is a guild member and if they have their DMs enabled. The DM includes an embed message containing the ban reason and server details.
  • Loading branch information
joeyaurel committed Jan 29, 2024
1 parent c9ed098 commit 2a0e5b9
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/cogs/team_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import discord
from discord import app_commands, User, Member
from discord.app_commands import CommandInvokeError
from discord.ext import commands
from discord.utils import MISSING

Expand Down Expand Up @@ -74,10 +75,44 @@ async def ban(self, interaction: discord.Interaction, user: discord.User, reason

await bans_channel.send(embed=embed)

await interaction.response.send_message('Du hast {} gebannt!'.format(
has_sent_dm = await self._send_ban_dm(interaction, user, reason)

await interaction.response.send_message('Du hast {} gebannt! ({})'.format(
user.mention,
has_sent_dm and 'DM gesendet.' or 'Keine DM gesendet.'
), ephemeral=True)

async def _send_ban_dm(self, interaction: discord.Interaction, user: discord.User, reason: str) -> bool:
try:
# Check if user is member of guild
member = interaction.guild.get_member(user.id)

if member is None:
return False

dm_channel = await member.create_dm()

dm_embed = discord.Embed(
title=f'{interaction.guild.name} - Ausschluss',
description=reason,
timestamp=interaction.created_at
)

if interaction.guild.icon is not None:
dm_embed.set_thumbnail(url=interaction.guild.icon.url)

await dm_channel.send(embed=dm_embed)

return True
except CommandInvokeError:
# User may not be in the guild, is not a member or has DMs disabled
pass
except Exception as e:
print(e)
pass

return False

@app_commands.command(
description='Einer Person die Vegan-Rolle vergeben',
)
Expand Down

0 comments on commit 2a0e5b9

Please sign in to comment.