95 lines
4.1 KiB
Python
95 lines
4.1 KiB
Python
import discord
|
|
|
|
async def execute(bot, params, message):
|
|
channel_name = params.get('channel_name')
|
|
dm_user_mention = params.get('dm_user')
|
|
content = params.get('message', '')
|
|
|
|
if not content.strip():
|
|
raise Exception("Le message ne peut pas être vide.")
|
|
|
|
guild = message.guild
|
|
sent_count = 0
|
|
|
|
if channel_name:
|
|
# Send to channel
|
|
# 1. Exact match
|
|
channel = discord.utils.get(guild.channels, name=channel_name)
|
|
|
|
# 2. Fuzzy match if not found (case insensitive, or partial match)
|
|
if not channel:
|
|
# Case insensitive
|
|
channel = discord.utils.find(lambda c: c.name.lower() == channel_name.lower(), guild.channels)
|
|
|
|
if not channel:
|
|
# Partial match (ignoring emojis/special chars often helps)
|
|
# Normalize: remove non-alphanumeric chars for comparison
|
|
def normalize(s):
|
|
return "".join(c for c in s if c.isalnum()).lower()
|
|
|
|
target_norm = normalize(channel_name)
|
|
if target_norm:
|
|
channel = discord.utils.find(lambda c: normalize(c.name) == target_norm, guild.channels)
|
|
|
|
if not channel:
|
|
# Last resort: contains
|
|
channel = discord.utils.find(lambda c: channel_name.lower() in c.name.lower(), guild.channels)
|
|
|
|
if not channel or not isinstance(channel, discord.TextChannel):
|
|
raise Exception(f"Salon '{channel_name}' introuvable.")
|
|
|
|
# Check permissions
|
|
if not channel.permissions_for(guild.me).send_messages:
|
|
raise Exception(f"Le bot n'a pas les permissions pour envoyer des messages dans #{channel.name}.")
|
|
await bot.messaging.send_message_with_limit(channel, content)
|
|
sent_count += 1
|
|
|
|
if dm_user_mention:
|
|
# Send DM to user
|
|
# Parse mention or ID or display name
|
|
user = None
|
|
if dm_user_mention.startswith('<@') and dm_user_mention.endswith('>'):
|
|
# Extract ID from mention
|
|
user_id_str = dm_user_mention.split('@')[-1].rstrip('>')
|
|
if user_id_str.isdigit():
|
|
user_id = int(user_id_str)
|
|
user = guild.get_member(user_id)
|
|
if not user:
|
|
try:
|
|
user = await guild.fetch_member(user_id)
|
|
except discord.NotFound:
|
|
raise Exception(f"Utilisateur introuvable via mention: {dm_user_mention}")
|
|
elif dm_user_mention.isdigit():
|
|
user_id = int(dm_user_mention)
|
|
user = guild.get_member(user_id)
|
|
if not user:
|
|
try:
|
|
user = await guild.fetch_member(user_id)
|
|
except discord.NotFound:
|
|
raise Exception(f"Utilisateur introuvable via ID: {dm_user_mention}")
|
|
else:
|
|
# Try to find by display name or username
|
|
user = discord.utils.find(lambda m: m.display_name == dm_user_mention or m.name == dm_user_mention, guild.members)
|
|
if not user:
|
|
# Try partial match (case insensitive)
|
|
dm_user_lower = dm_user_mention.lower()
|
|
user = discord.utils.find(lambda m: dm_user_lower in m.display_name.lower() or dm_user_lower in m.name.lower(), guild.members)
|
|
if not user:
|
|
raise Exception(f"Utilisateur introuvable via nom: {dm_user_mention}")
|
|
|
|
try:
|
|
await user.send(content)
|
|
sent_count += 1
|
|
except discord.Forbidden:
|
|
raise Exception(f"Impossible d'envoyer un DM à {dm_user_mention}. L'utilisateur a peut-être bloqué le bot.")
|
|
|
|
if sent_count == 0:
|
|
raise Exception("Au moins un destinataire (channel_name ou dm_user) doit être spécifié.")
|
|
|
|
# If both were specified and successful
|
|
if channel_name and dm_user_mention and sent_count == 2:
|
|
await message.channel.send(f"Message envoyé dans #{channel_name} et en DM à {dm_user_mention}.")
|
|
elif channel_name:
|
|
await message.channel.send(f"Message envoyé dans #{channel_name}.")
|
|
elif dm_user_mention:
|
|
await message.channel.send(f"Message envoyé en DM à {dm_user_mention}.")
|