I tried using cogs and it didnt go smoothly as expected since this is my first time using it. I was transferring my codes into their own cog and decided to run the bot for a test. One of my command failed to work and i was confused as it works before transferring. I am really new to nextcord.py please help me and acknowledge me.
the code in the cog:
import nextcord
from nextcord.ext import commands
from nextcord import client
from nextcord.utils import get
from async_timeout import asyncio
import random
class King_Game(commands.Cog, name="King's Game"):
def __init__(self, bot:commands.Bot):
self.bot = bot
@commands.command()
async def king(self, ctx, mode=None):
time = 10
ktime = 2
wtime = 1
if mode == "1f":
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Follower", description="The **follower** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower = random.choice(remains)
print (follower)
fembed = nextcord.Embed(title="Follower", description=f"{follower.mention} is the **follower**!", color=0x2f9e4d)
fembed.set_footer(text="The follower must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
elif mode == "2f":
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Followers", description="The **followers** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower1 = random.choice(remains)
follower2 = random.choice(remains)
print (follower1, follower2)
fembed = nextcord.Embed(title="Follower", description=f"{follower1.mention} and {follower2.mention} are the **followers**!", color=0x2f9e4d)
fembed.set_footer(text="The followers must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
elif mode == None:
await ctx.send("Please specify the amount of followers. `1f or 2f`")
else:
await ctx.send("Mode is not supported, game will start with only 1 follower by default.")
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Follower", description="The **follower** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower = random.choice(remains)
print (follower)
fembed = nextcord.Embed(title="Follower", description=f"{follower.mention} is the **follower**!", color=0x2f9e4d)
fembed.set_footer(text="The follower must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
def setup(bot: commands.Bot):
bot.add_cog(King_Game(bot))
the error:
Ignoring exception in command king:
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 168, in wrapped
ret = await coro(*args, **kwargs)
File "d:\Bitter\VCS\Test-Bot-\Modules\kinggame\codes.py", line 28, in king
players.pop(players.index(client.user))
AttributeError: module 'nextcord.client' has no attribute 'user'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1055, in invoke
await ctx.command.invoke(ctx)
File "C:\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 933, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 177, in wrapped
raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: module 'nextcord.client' has no attribute 'user'
before transferring:
@client.command()
async def king(ctx, mode=None):
time = 10
ktime = 2
wtime = 1
if mode == "1f":
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Follower", description="The **follower** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower = random.choice(remains)
print (follower)
fembed = nextcord.Embed(title="Follower", description=f"{follower.mention} is the **follower**!", color=0x2f9e4d)
fembed.set_footer(text="The follower must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
elif mode == "2f":
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Followers", description="The **followers** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower1 = random.choice(remains)
follower2 = random.choice(remains)
print (follower1, follower2)
fembed = nextcord.Embed(title="Follower", description=f"{follower1.mention} and {follower2.mention} are the **followers**!", color=0x2f9e4d)
fembed.set_footer(text="The followers must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
elif mode == None:
await ctx.send("Please specify the amount of followers. `1f or 2f`")
else:
await ctx.send("Mode is not supported, game will start with only 1 follower by default.")
embed = nextcord.Embed(title="King's Game", description="React to join.", color=0xfffb00)
embed.set_footer(text=f"Ends in {time}s")
embed.set_image(url="https://i.imgur.com/Xcm8MPn.gif")
kmsg = await ctx.send(embed=embed)
await kmsg.add_reaction("👑")
await asyncio.sleep(time)
new_msg = await ctx.channel.fetch_message(kmsg.id)
players = await new_msg.reactions[0].users().flatten()
players.pop(players.index(client.user))
print (players)
king = random.choice(players)
tembed = nextcord.Embed(title="Time is up!", description="Get ready to obey the king.",colour=0x54fffc)
await ctx.send(embed=tembed)
await asyncio.sleep(ktime)
kembed = nextcord.Embed(title="King", description=f"{king.mention} is the **king**!", color=0xff1100)
kembed.set_image(url="https://c.tenor.com/CVkAWrZvFAMAAAAC/sinister-smile.gif")
await kmsg.remove_reaction(emoji="👑", member=king)
await ctx.send(embed=kembed)
wembed = nextcord.Embed(title="Follower", description="The **follower** will be revealed shortly.", colour=0x54fffc)
wembed.set_footer(text="Get ready.")
await asyncio.sleep(wtime)
await ctx.send(embed=wembed)
fmsg = await ctx.channel.fetch_message(kmsg.id)
remains = await fmsg.reactions[0].users().flatten()
remains.pop(remains.index(client.user))
print (remains)
follower = random.choice(remains)
print (follower)
fembed = nextcord.Embed(title="Follower", description=f"{follower.mention} is the **follower**!", color=0x2f9e4d)
fembed.set_footer(text="The follower must obey the king's order.")
fembed.set_image(url="https://cdn.discordapp.com/attachments/906110784822738984/934469807506620476/follower.gif")
await asyncio.sleep(ktime)
await ctx.send(embed=fembed)
That is because the client
object you're using is not the discordpy client object, but in fact nextcord.client
module you've imported.
from nextcord import client
It seems like self.bot object is what you're looking for. nextcord.commands.Bot
object is inherited from nextcord.Client
and thus does have user
attribute.
You can change every client.user
in your code to self.bot.user
.