【问题标题】:bot.get_channel() Not working in the Cogsbot.get_channel() 在 Cogs 中不起作用
【发布时间】:2020-06-15 13:15:16
【问题描述】:

这在没有 COG 的情况下可以完美运行,但在 COG 中它不起作用并产生以下错误: NameError: 名称 'bot' 未定义

import discord
from discord.ext import commands

class channelinfo(commands.Cog):
    #@commands.Cog.listener() [EVENT]
    #@commands.command() [COMMAND]
    def init(self, bot):
        self.bot = bot

    @commands.command()
    async def channelinfo(self,ctx,*,val:str = None):
        val = val.replace('<','')
        val = val.replace('>','')
        val = val.replace('#','')
        print(val)
        channel = await bot.get_channel(int(val))

即使是 discord.User 也不在 COG 中工作。 错误:命令引发异常:AttributeError:“用户”对象没有属性“角色”

    @commands.command()
    async def userinfo(self,ctx,user:discord.User = None):
        msg = ''
        for a in user.roles:
            msg+= a.name

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    你试过了吗

    channel = await self.bot.get_channel(int(val))
    

    【讨论】:

    • channel = await self.bot.get_channel(int(val)) AttributeError: 'channelinfo' object has no attribute 'bot'
    • 好吧,我明白了,你用 init 代替了 init
    • __ init __ 没有空格
    • bot.get_channel(id) 不是协程,bot.fetch_channel(id) 是。
    猜你喜欢
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2019-04-30
    • 2021-04-19
    • 2022-08-14
    • 2020-09-26
    • 2021-05-22
    相关资源
    最近更新 更多