【发布时间】:2018-05-21 22:19:41
【问题描述】:
你好,我收到了错误
File "C:\Users\Tom\Documents\bot\cogs\misccoms.py", line 28, in giveRole
example = discord.utils.get(ctx.message.server.roles,name='Example')
AttributeError: 'Misccoms' object has no attribute 'message'
从这行代码
@commands.command(pass_context=True, no_pm=True)
async def giveRole(ctx):
example = discord.utils.get(ctx.message.server.roles,name='Example')
await self.bot.add_roles(ctx.message.mentions[0], example)
该命令的功能应该是在成员上放置一个角色,在这种情况下角色是Example。
完整代码如下
import discord
from discord.ext import commands
from .utils import checks
class Misccoms:
"""Misccoms"""
def __init__(self, bot):
self.bot = bot
#Tool Commands
@commands.command(pass_context=True, no_pm=True)
async def giveRole(ctx):
example = discord.utils.get(ctx.message.server.roles,name='Example')
await self.bot.add_roles(ctx.message.mentions[0], example)
def setup(bot):
bot.add_cog(Misccoms(bot))
我不确定我在哪里出错了。
【问题讨论】:
-
代码期望您在
Misccoms类中有变量message- 但您没有。也许您必须使用其他类来定义class Misccoms(some_other_class): -
我认为您缺少
self参数作为giveRole中的第一个参数。您的代码将ctx解释为Misccoms的实例,因为它是方法中的第一个参数。
标签: python discord discord.py