I have the unban function in my disnake bot. After I unban, the bot should send a personal message to the user, but I got an error there.
Code:
@bot.slash_command(name="unban",
description="Разбан пользователя (указание причины обязательно)"
)
@commands.has_permissions(ban_members=True)
async def unban(inter, member_id: str, reason: str):
user = await bot.get_or_fetch_user(int(member_id))
await inter.response.send_message(f'{user.mention} разбанен по причине: "{reason}".')
await bot.guilds[0].unban(user, reason = reason)
ban_list_channel = bot.get_channel(1274693427811844157)
notification_channel = bot.get_channel(1274130140489580584)
unban_embed = disnake.Embed(title = 'Пользователь разбанен!',
description = f'{user.mention} разбанен на сервере.n{ban_list_channel.mention} обновлен.',
color = disnake.Colour.yellow(),
timestamp= datetime.now()
)
unban_embed_for_user = disnake.Embed(title = 'Вы были разбанены!',
description = f'{user.mention} разбанен на сервере {bot.guilds[0].name}.nПричина: "{reason}"nПриглашаем вас: https://discord.gg/hwQM6GG4kB',
color = disnake.Colour.green(),
timestamp= datetime.now()
)
lines = []
with open('source/txt/ban_list.txt', 'r') as ban_list:
line = ban_list.readline()
if str(member_id) not in line:
lines.append(line)
while line:
line = ban_list.readline()
if str(member_id) not in line:
lines.append(line)
with open('source/txt/ban_list.txt', 'r+') as ban_list:
ban_list.truncate()
for line in lines:
ban_list.write(line)
text = ban_list.read()
await notification_channel.send(embed = unban_embed)
await ban_list_channel.purge(limit=3)
ban_list_embed = disnake.Embed(title = 'Список забаненных пользователей:',
description = text,
color = disnake.Colour.red(),
timestamp= datetime.now()
)
await ban_list_channel.send(embed = ban_list_embed)
await asyncio.sleep(3)
await inter.delete_original_message()
await user.send(embed = unban_embed_for_user)
This is the error:
Ignoring exception in slash command 'unban':
Traceback (most recent call last):
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeextcommandsslash_core.py", line 728, in invoke
await call_param_func(self.callback, inter, self.cog, **kwargs)
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeextcommandsparams.py", line 1083, in call_param_func
return await maybe_coroutine(safe_call, function, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeutils.py", line 567, in maybe_coroutine
return await value
^^^^^^^^^^^
File "D:ds_bottrader.py", line 172, in unban
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeabc.py", line 1748, in send
data = await state.http.send_message(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakehttp.py", line 409, in request
raise Forbidden(response, data)
disnake.errors.Forbidden: 403 Forbidden (error code: 50007): Cannot send messages to this user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeextcommandsinteraction_bot_base.py", line 1378, in process_application_commands
await app_command.invoke(interaction)
File "C:UsersIcoldAppDataLocalProgramsPythonPython311Libsite-packagesdisnakeextcommandsslash_core.py", line 737, in invoke
raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50007): Cannot send messages to this user
I tried to add a sleep
function because I thought that the response from the server wasn’t delivered yet, but this thought is wrong.
I have three opinion,
User’s Privacy Settings: The user might have their privacy settings configured to disallow DMs from server members who are not friends. Or maybe The user might have blocked the bot, preventing it from sending DMs. maybe The user might have disabled receiving DMs from members of the server.
Have you update the disnake? It can be a library issue.
bursow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1