如何使用Discord.py将消息发送到特定频道?

时间:2020-06-14 15:14:19

标签: python discord discord.py

现在,我想制作一个记录所有已发送内容并将其发送到聊天记录的记录器机器人。我现在有以下代码:

import discord
client = discord.Client()
@client.event
async def on_ready():
    print("The bot is ready!")
    await client.change_presence(activity = discord.Game(name = "Logging whatever you say"))
@client.event
async def on_message(message):
    channel = client.get_channel("721085548868927560")
    sender_id = message.author.id
    message_content = message.content
    if message.author == client.user:
        return
    else:    
        await channel.send("<@" + sender_id + "> said " + message_content)
client.run("Not your bot, so don't look at the API token")

只要它运行并且有人说了什么,就会出现错误

  File "C:\Users\ryany.SVCYINBOOK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "logger.py", line 15, in on_message
    await channel.send("<@" + sender_id + "> said " + message_content)
AttributeError: 'NoneType' object has no attribute 'send'

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

如Josh在评论中所指出的,IDs/Snowflakes are ints in rewrite

我建议您仔细阅读rewrite中的主要更改,以使您熟悉重写的行为。它还可以帮助您在联机查看时识别异步(v0.16.x,现已弃用)和重写(v1.x)代码之间的区别,并且偶然发现如何做事的示例。


参考: