Reputation: 626
When I do my discord.py project, I found out that the following codes work well in the first time but return an attribute error for the second time (in a single run).
class Test2(commands.Cog):
def __init__(self, dc_bot):
self.bot = dc_bot
self.ui = ""
@commands.command(description="get avatar")
async def getavatar(self, ctx, *, content: str):
avatar = self.bot.user.avatar_url
self.ui += content
await ctx.send(content)
await ctx.send(avatar)
self.__init__(self) # reset the values
First time it works well.
Second time it will say: AttributeError: 'Test2' has no attribute 'user'
I guess because I want to reset self.ui for the next run.
And if there are a lot of "self" in my init function that I need to use, I thought (before) that it is a good idea to just called the init function. But running again self.ui = dc_bot
will cause this problem I think. Could you explain why this would happen please?
Upvotes: 1
Views: 464
Reputation: 33714
There's a mistake when you re-calls the init method. self.__init__(self)
is actually calling Test2. __init__(self, self)
. Which overrides self.bot = self
thus the attribute error when you run the command a second time. Instead you want:
self.__init__(self.bot)
But this isn't a good solution, instead you should have a "reset" helper method that does the resetting,and you call the helper method instead of init. Since normally, once the class is initiated, you don't want to call the init method again.
Upvotes: 1
Reputation: 98
You are making the code way too complex try this out!
@commands.command(description = "get avatar")
async def getavatar(self, ctx, *, avamember: discord.Member = None):
userAvatarUrl = avamember.avatar_url
await ctx.send(userAvatarUrl)
Upvotes: 0