2017-12-07 45 views
0

Hallo Ich erhalte den FehlerObjektfehler Attribut „Objekt hat kein Attribut‚message‘“

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' 

Aus dieser Zeile Code

@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) 

Die Funktion dieses Befehls an Stelle soll Eine Rolle auf einem Mitglied in diesem Fall ist die Rolle Example.

Der vollständige Code wie folgt

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)) 

Ich bin nicht sicher, wo ich falsch hier werde.

+0

Code erwartet, dass Sie Variable "Nachricht" in der Klasse "Misccoms" haben - aber Sie haben es nicht. Vielleicht musst du eine andere Klasse benutzen, um 'class Misccoms (some_other_class) zu definieren:' – furas

+0

Ich glaube, du verpasst einen 'self' Parameter als ersten Parameter in' giveRole'. Ihr Code interpretiert "ctx" als eine Instanz von "Misccoms", da es der erste Parameter in einer Methode ist. – bunji

Antwort

0

Ich glaube, das Problem ist, den 'self' Parameter vor dem 'ctx' Parameter wie folgt hinzuzufügen. Ich habe es getestet und es hat für mich funktioniert.

import discord 
from discord.ext import commands 

class GiveRole(): 
    def __init__(self, bot): 
     self.bot = bot 

    @commands.command(pass_context=True, no_pm=True) 
    async def giveRole(self, ctx): 
     example = discord.utils.get(ctx.message.server.roles,name='ExampleRole') 
     await self.bot.add_roles(ctx.message.mentions[0], example) 

def setup(bot): 
    bot.add_cog(GiveRole(bot)) 
Verwandte Themen