Im Still New (Because I Took A Break And Forgot Almost Everything). I Was Coding An AFK Bot And This Happened...
Error: MissingRequiredArgument: context is a required argument that is missing.
Code:
#client.command()
async def start(ctx,context,user: discord.Member):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
name = context.author.display_name
author = context.author
guild = context.guild
AFKrole = discord.utils.get(guild.roles, name="AFK")
if not AFKrole:
AFKrole = await guild.create_role(name="AFK")
await context.author.edit(nick=f"[AFK]{name}")
await author.add_roles(AFKrole)
await ctx.send(f"AFK Mode Started For **{author}**.")
gt = await client.wait_for('message', check=check, timeout=180)
if gt.content.lower() == "afk stop":
await context.author.edit(nick=f"{name}")
await author.remove_roles(AFKrole)
await ctx.send(f"AFK Mode Stopped For **{author}**.")
else:
pass
please help.
Remove context parameter and replace any context part in the code with just ctx. ctx is already context.
Ex.
name = ctx.author.display_name
author = ctx.author
guild = ctx.guild
Related
I am trying to create a command wherein if you react on the embed, the bot sends something back. If I add this code to a cog, it won't work. If possible, can you tell me why?
#bot.command(aliases=['test','t'])
async def Test(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
With the help of the people in the comment section, I have found the problem with my code. I had to change bot.command into commands.command. (I've tried both bot and command, and it still works splendidly). The other crucial thing I had to add was "self" under bot.wait_for. Without the self, the command wouldn't work. Thank you so much for the help.
#commands.command(aliases=['test','t'])
async def Testing(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await self.bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
you need #commands.command instead of #bot.command
I have this:
#commands.command()
async def destroyX(self, ctx):
await ctx.message.delete()
for channel in list(ctx.guild.channels):
try:
await channel.delete()
except:
pass
for user in list(ctx.guild.members):
try:
await user.ban()
except:
pass
for role in list(ctx.guild.roles):
await role.delete()
else:
await ctx.send('You are not allowed to execute this command!')
Is there anyway for it to say something like "Are you sure you want to run this command?" in the chat.
You can use an if-else statement:
#commands.command()
async def destroyX(self, ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
await ctx.message.delete()
await ctx.send("Are you sure you want to run this command?")
# for user input
response = await self.bot.wait_for('message', check=check)
if response.content == 'yes':
# the actions which should happen if the person responded with 'yes'
else:
# actions which should happen if the person responded with 'no' or something else
I am trying to add roles in discord.py but I can't really make it work.
#bot.command(brief="Report member")
async def member(ctx):
if ctx.author.id != 783430063076147210:
await ctx.send("Mention the member you want to report")
message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)
msg = message.content.replace("<","")
msg = msg.replace(">","")
msg = msg.replace("#","")
msg = msg.replace("!","")
#try:
msg = int(msg)
user = bot.get_user(msg)
await ctx.send('Are you sure you want to report ' + user.mention + "?")
message = await bot.wait_for('message', check=lambda message: ctx.author == ctx.author)
if message.content.lower() == "yes" or message.content.lower() == "y":
member = ctx.message.author
role = get(member.guild.roles, name="Reported")
user = ctx.guild.get_member(msg)
await bot.add_roles(user, role)
await ctx.send(user.mention + " was reported by " + member.mention + ".")
else:
await ctx.send("User was not reported!")
#except:
await ctx.send("Oops! That was not a user!")
I'm getting an AttributeError: 'Bot' object has no attribute 'add_roles', and the same with the other way
await user.add_roles(role)
returns AttributeError: 'NoneType' object has no attribute 'add_roles'
Is there something I'm missing?
Two ways of making this work:
1.
#bot.command()
async def role(ctx, member:commands.MemberConverter, role:commands.RoleConverter):
await member.add_roles(role)
^^ This method uses a role converter that accepts role ID, name, or mention.
I also use commands.MemberConverter because it accepts more methods of referencing a user than discord.Member, and the same thing for discord.Role.
2.
#bot.command()
async def role(ctx, member:commands.MemberConverter, role):
role = discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(role)
^^ This method uses discord.utils.get to get a guild role. From my knowledge, it only accepts role names and not role IDs or mentions.
bot.get_user() will return a User object, to which you cannot add roles, as no guild is specified. You could do ctx.guild.get_member(id) to get the Member object then add roles to that, but you'd be better of getting the member from the mentions attribute of a Message. Docs: https://discordpy.readthedocs.io/en/latest/api.html?highlight=mentions#discord.Message.mentions
Try something like this:
#bot.command()
async def role(ctx):
test = ctx.author
role = discord.utils.get(test.guild.roles, name="Name")
if role in test.roles:
await ctx.send("you already have a role")
else:
await test.add_roles(role)
Your line
await bot.add_roles(user, role)
needs to be replaced by
await user.add_roles(role)
these are my addrole and remouverole commands:
#bot.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, member : discord.Member, role : discord.Role):
await member.add_roles(role)
await ctx.send(f"{role} is added to {member}.")
#bot.command()
#commands.has_permissions(administrator=True)
async def removerole(ctx, member : discord.Member, role : discord.Role):
await member.remove_roles(role)
await ctx.send(f"{role} is removed from {member}.")
Hey guys!
I am trying to make a litte ticket-bot for my server - the problem is that the emoji in the reaction deletes a channel!
So if a member uses the emoji outside the new created channel it will delete the channel :(
What does my code look like now:
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
guild_id = payload.guild_id
guild = self.client.get_guild(guild_id)
user_id = payload.user_id
user = self.client.get_user(payload.user_id)
message_id = payload.message_id
emoji = payload.emoji.name
if message_id == 768765161301213185 and emoji == "📩":
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
support_role = guild.get_role(760792953718308915)
category = guild.get_channel(768764177736400906)
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
support_role: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
ticket_nr = random.randint(100,999)
channel = await category.create_text_channel(f'ticket-{ticket_nr}', overwrites=overwrites)
embed = discord.Embed(
title="How can I help you?",
description="Please wait for a supporter.")
embed.set_author(name="TiLiKas Ticket Bot")
for channel_all in guild.text_channels:
if str(channel_all) == str(channel):
if user_id != 739740219544305714 and emoji == "🔒":
await channel.delete(reason=None)
else:
print("ERROR")
What do I want?
I want the bot only to responde on the emoji if its used in the new created channel!
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=check#discord.ext.commands.Bot.wait_for
#client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')
Your check method should look something like:
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍' and reaction.message.channel == message.channel
This check here, checks:
the user who is reacting to the message is the same user who started the command.
the reaction is a thumbs up emoji.
the reaction is done in the same channel the command was started in.
If user add reaction :HotS_Tank: in a special message, the bot will need to give this role to user, but I do not have any idea how to do it...
That's what I have tried:
async def role_background_task():
await client.wait_until_ready()
roleChannel = discord.Object(id='411270826374070293')
roleMSG1 = client.get_message(roleChannel, id='411657109860515840')
roleMSG2 = client.get_message(roleChannel, id='411657144488689674')
while not client.is_closed:
reac1 = await client.wait_for_reaction(emoji=':HotS_Tank:411445724287598592',message=roleMSG1)
if reac1.reaction.emoji == ':HotS_Tank:411445724287598592':
await client.add_roles(reac1.user, roleHOTS_Tank)
client.loop.create_task(role_background_task())
If you check the documentation, there's an event called on_reaction_add here. You can simply use that.
#client.event
async def on_reaction_add(reaction, user):
roleChannelId = '411270826374070293'
if reaction.message.channel.id != roleChannelId:
return #So it only happens in the specified channel
if str(reaction.emoji) == "<:HotS_Tank:411445724287598592>":
await client.add_roles(user, roleHOTS_Tank)