Z
Z
Zakhar2020-08-05 16:09:34
Python
Zakhar, 2020-08-05 16:09:34

How to send a message to a chat by chat ID - discord.py rewrite?

Hello, here I have a code by which messages should be sent to the channel indicated in the message:

@bot.command()
async def news(ctx,channel_id, text):
    emb= discord.Embed(title='Новость!!!',description=f'{text}', timestamp=ctx.message.created_at)
    channel= bot.get_channel(channel_id)
    await channel.send(embed=emb)

I wrote on one of your questions, but it gives an error:
Ignoring exception in command news:
2020-08-05T12:54:20.983670+00:00 app[worker.1]: Traceback (most recent call last):
2020-08-05T12:54:20.983698+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
2020-08-05T12:54:20.983698+00:00 app[worker.1]:     ret = await coro(*args, **kwargs)
2020-08-05T12:54:20.983701+00:00 app[worker.1]:   File "Bot.py", line 248, in news
2020-08-05T12:54:20.983702+00:00 app[worker.1]:     await channel.send(embed=emb)
2020-08-05T12:54:20.983729+00:00 app[worker.1]: AttributeError: 'NoneType' object has no attribute 'send'
2020-08-05T12:54:20.983732+00:00 app[worker.1]: 
2020-08-05T12:54:20.983733+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2020-08-05T12:54:20.983733+00:00 app[worker.1]: 
2020-08-05T12:54:20.983736+00:00 app[worker.1]: Traceback (most recent call last):
2020-08-05T12:54:20.983770+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
2020-08-05T12:54:20.983770+00:00 app[worker.1]:     await ctx.command.invoke(ctx)
2020-08-05T12:54:20.983771+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
2020-08-05T12:54:20.983771+00:00 app[worker.1]:     await injected(*ctx.args, **ctx.kwargs)
2020-08-05T12:54:20.983774+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
2020-08-05T12:54:20.983775+00:00 app[worker.1]:     raise CommandInvokeError(exc) from exc
2020-08-05T12:54:20.983813+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'


And if you write:
@bot.command()
async def news(ctx,channel_id, text):
    emb= discord.Embed(title='Новость!!!',description=f'{text}', timestamp=ctx.message.created_at)
    channel= bot.get_channel(channel_id)
    await ctx.channel.send(embed=emb)

That will be sent to the same channel in which the command was written

. Is it possible to write something to a friend?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-08-05
@xzartsust

bot.get_channelaccepts an ID in the int type as an argument. By default, all arguments passed to the command by the parser are passed there as str, unless another type is specified. In your case, this function will never return a channel.
The most adequate solution in the context of discord.py: Use converters .

@bot.command()
async def news(ctx, channel: discord.TextChannel, *, text):  # используем kwargs для отсутствия необходимости написания ковычек: https://discordpy.readthedocs.io/en/stable/ext/commands/commands.html#keyword-only-arguments
    emb= discord.Embed(title='Новость!!!',description=f'{text}', timestamp=ctx.message.created_at)
    await channel.send(embed=emb)

This will allow you to get a channel in a team not only by ID, but also by its mention or just by name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question