【问题标题】:Bot Discord Python : Problem with automatic sending of messagesBot Discord Python:自动发送消息的问题
【发布时间】:2021-03-15 04:41:14
【问题描述】:

我的目标是创建一个 Discord 机器人,在特定时间和日期向房间发送消息。不幸的是,我遇到了一个问题,我的 python 机器人声明了两个错误:Task exception was never retrieved future: <Task finished name = 'Task-1' coro = <job () done""await client.send (message, channel) AttributeError: 'Client' object has no attribute 'send' AttributeError: 'Client' object has no attribute 'send',尽管机器人启动了,但它没有发送所需的消息。

这是我的代码:

bot = commands.Bot(command_prefix='!')

client = discord.Client()

@bot.event
async def on_ready():
    print("Hello !")

@bot.event
async def job():
    channel = client.get_channel(ID)
    message = ('Hello !')
    await client.send(message, channel)

    sched = BlockingScheduler()
    sched.add_job(job, 'cron', month='1-12', day_of_week='mon-sun', hour='0-23')
    sched.start()
    
client.loop.create_task(job())
   
bot.run(TOKEN)

提前感谢您的帮助!

【问题讨论】:

    标签: python discord.py bots


    【解决方案1】:

    您可以使用channel.send 方法直接使用频道,而不是client.send

    async def job():
        channel = client.get_channel(ID)
        message = "Hello !"
        await channel.send(message)
    
        # rest of your code here.
    

    参考:

    TextChannel#send

    【讨论】:

    • 感谢您的回答,但不幸的是,问题仍然存在并显示相同的第一个错误,但第二个显示:"AttributeError: 'NoneType' object has no attribute 'send'"
    • 这可能意味着该频道不存在。
    猜你喜欢
    • 2020-12-01
    • 2020-11-07
    • 2021-08-12
    • 2021-03-02
    • 2021-01-12
    • 2021-06-05
    • 2022-01-05
    • 2021-05-03
    • 2018-05-03
    相关资源
    最近更新 更多