【问题标题】:Python3 + Discord - Send a message each X secondsPython3 + Discord - 每 X 秒发送一条消息
【发布时间】:2020-12-29 06:20:17
【问题描述】:

我正在尝试创建一个 Discord 机器人,它会每 X 秒(例如每 3 秒)自动发送一条消息,而无需任何用户交互/命令输入。

这是我的代码:

import discord
from discord.ext import tasks, commands

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

@bot.event
async def on_ready():
    await bot.wait_until_ready()
    print("Logged in as:")
    print(bot.user.name)
    print("------")
    channel = bot.get_channel(IDasInteger)
    print("Channel is:")
    print(channel) #Prints None
    get_price.start()

@tasks.loop(seconds=3)
async def get_price():
    await bot.wait_until_ready()
    channel = bot.get_channel(IDasInteger)
    print("Channel is:")
    print(channel) #Prints None
    await channel.send('Test')

@get_price.before_loop
async def before ():
    print("Before done.")

bot.run('MyTokenHere')

问题是,当我执行这段代码时,它给了我以下错误:

AttributeError: 'NoneType' object has no attribute 'send'

当我尝试打印通道变量时,它返回无。 频道 ID 是正确的 - 我直接从 Discord 应用程序复制了它,没有对其值进行任何更改。

有什么想法吗?

谢谢

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您得到None 的原因是IDasInteger 未定义(至少在您提供的代码中。如果它在您的代码中定义,则它找不到通道并返回NoneNone没有属性send,报错。

    将频道 ID 更改为您服务器中有效频道的 ID。

    functions channel = bot.get_channel(790279425633758682)

    或者确保 IDasInteger 返回一个有效的频道 ID

    注意:我用有效的频道 ID 对我的服务器的代码进行了测试,它按预期工作。

    【讨论】:

    • 这是问题,因为我使用了错误的频道 ID。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2018-06-27
    • 2023-03-22
    • 2022-08-19
    相关资源
    最近更新 更多