W
W
wetwixs2020-07-04 01:41:10
Python
wetwixs, 2020-07-04 01:41:10

Error when sending a message to private messages?

Hello. I encountered such a problem that the bot does not send the necessary messages to the LAN, in my case it is the same notification about the mute. As I understand it, due to the fact that a person simply has a closed LS.

The code:

@client.command()
@commands.has_any_role( 703596629894365209, 703596629894365210, 703596629894365211, 703596629923725334, 703596629911142433 )
async def mute( ctx, member:discord.Member,time:int, * ,reason ):
  logchannel = client.get_channel( 703596630892478505 )
  chatmoder = client.get_channel( 703596630892478509 )
  generalchat = client.get_channel( 703596631060381711 ) 
  muterole = discord.utils.get( ctx.guild.roles,id=718822379190747226 )
  emb = discord.Embed( title="Выдача заглушки пользователю",color=0xff0000 )
  emb.add_field(name='Модератор', value=ctx.message.author.mention)
  emb.add_field(name='Нарушитель', value=member.mention)
  emb.add_field(name='Причина', value=reason, inline = False)
  emb.add_field(name='Время',value=time, inline = False)
  await member.send(f'{member.name},`Вы были заглушены модератором` {ctx.author.name} `по причине` - {reason}. `Время мута` - {time} `минут`\n`Если не согласны с вердиктом - Вы в праве написать жалобу` - https://forum.robo-hamster.ru/forums/114/')
  await member.add_roles(muterole)
  await logchannel.send(embed = emb)
  await chatmoder.send(f'`Мут нарушителю` - {member.mention} `был успешно выдан на` - {time} `минут`')
  await generalchat.send(f'{member.mention}, `Вы были заглушены модератором` {ctx.author.name} `по причине` - {reason} `на` {time} минут')
  await asyncio.sleep(time * 60)
  await member.remove_roles(muterole)


Mistake:
Ignoring exception in command mute:
Traceback (most recent call last):
  File "C:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot.py", line 50, in mute
    await member.send(f'{member.name},`Вы были заглушены модератором` {ctx.author.name} `по причине` - {reason}. `Время мута` - {time} `минут`\n`Если не согласны с вердиктом - Вы в праве написать жалобу` - https://forum.robo-hamster.ru/forums/114/')
  File "C:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\abc.py", line 856, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed, nonce=nonce)
  File "C:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\http.py", line 221, in request
    raise Forbidden(r, data)
discord.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:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Wetwi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50007): Cannot send messages to this user


As I understand it, I need to check if the person’s PM is closed, then the bot itself would simply skip the item with await member.send and continue the code, but I didn’t understand how to do it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kactus, 2020-07-04
@wetwixs

I don’t know how with await, but you can try with try and except

W
wetwixs, 2020-07-04
@wetwixs

Problem fixed. Yes, it was necessary through try and except.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question