【问题标题】:Python Discord.py Send message in channel from cogPython Discord.py 从 cog 在通道中发送消息
【发布时间】:2021-02-19 15:47:48
【问题描述】:

我正在尝试改编文档中的代码。我想每 5 秒向特定频道发送一条消息,但我无法访问它始终返回 None 的频道。

class MyCog(commands.Cog):
    def __init__(self,bot):
        self.index = 0
        self.task.start()
        self.bot = bot

    @tasks.loop(seconds=5.0)
    async def task(self):
        await self.bot.get_guild("").get_channel("").send("Test")
        print(self.index)

        self.index += 1

def setup(bot):
    bot.add_cog(MyCog(bot))

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    它总是返回None,因为你正在这样做:

    await self.bot.get_guild("").get_channel("")
    

    你需要传入实际的Guild id & Channel id。

    await self.bot.get_guild(guild_id_goes_here).get_channel(channel_id_goes_here)
    

    还要确保 id 是 ints,而不是 strings,并且您的机器人可以看到相关的公会和频道。

    【讨论】:

    • AttributeError: 'NoneType' 对象没有属性 'get_channel'
    • 这意味着您的机器人无法看到具有该 ID 的公会。您确定您复制的 ID 正确吗?
    猜你喜欢
    • 2021-05-18
    • 2022-01-22
    • 1970-01-01
    • 2021-06-06
    • 2020-10-12
    • 2021-04-04
    • 2022-01-21
    • 2021-05-03
    • 2021-07-08
    相关资源
    最近更新 更多