So I wrote this command which I thought would send an invite to the member that was tagged but apparently I am missing something.
It returns this error:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Invite' object has no attribute 'ctx'
Thanks.
import discord
from discord.ext import commands
class Invite(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command(pass_context=True)
async def invite(self, ctx, member: discord.Member, *argument):
link = await ctx.channel.create_invite(max_uses=1, unique=True)
await member.send(link)
await ctx.send(f'{member.name} has received an invite.')
def setup(client):
client.add_cog(Invite(client))
I guess you have to request the invite in another way.
Try out the following:
#commands.command(pass_context=True)
async def invite(self, ctx, member: discord.Member, total: int = None):
guild = self.client.get_channel(ctx.channel.id)
link = await guild.create_invite(max_uses=1 if total is not None else total, unique=True)
await member.send(link)
await ctx.send(f'{member.name} has received an invite.')
What did we do?
Get the channel.id and define it as guild
Create an invite to guild with max. 1 use and the property of being unique
Send the link to the member and confirm it by bot
Related
Alright I am trying to create a command where the user does (prefix)requestrole discordid roleid. It then sends a request embed to a channel where certain roles can view that channel and they can click on either the checkmark reaction or the 'x' reaction to accept or deny the role request.
#commands.command()
async def requestrole(self, ctx, discordid, roleid):
discordid = int(discordid)
roleid = int(roleid)
userid = ctx.author.id
channel = self.bot.get_channel(FILLWITHCHANNELID)
guild = ctx.guild
user = ctx.author
role = get(guild.roles, id=roleid)
embed = discord.Embed(title="Role Request", description=f"{user} requested {role} for {userid}", color=0x00ff00)
await channel.send(embed=embed)
message = await embed.send(embed=embed)
await message.add_reaction("✅")
await message.add_reaction("❌")
#self.bot.event
async def on_reaction_add(reaction, user):
if reaction.emoji == "✅":
user = reaction.message.author
guild = reaction.message.guild
role = get(guild.roles, id=roleid)
await user.add_roles(role)
await channel.send(f"{user} has been given {role}")
elif reaction.emoji == "❌":
await reaction.message.send("Role request denied")
This is the error I am now receiving
Ignoring exception in command requestrole:
Traceback (most recent call last):
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\name\Documents\GitHub\PRP-Bot\cogs\role_cog.py", line 31, in requestrole
message = await embed.send(embed=embed)
AttributeError: 'Embed' object has no attribute 'send'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Embed' object has no attribute 'send'
My question particularly is what is causing the error, and how do I properly code it so a person can basically click on the reaction checkmark to add the role, or click the x to remove the role.
you are trying to send a message to an Embed. That's not how Embeds work. you can send a message to a channel but not to a Embed. Just remove the line message = await embed.send(embed=embed) and you should be fine
I am trying to write a command that can ban a user Using ID and #. But there is a problem: the bot cannot ban a user if he is not on the server.
There's code:
async def ban(ctx, member: discord.Member = None, *, reason=None):
await member.ban()
await ctx.send("You have banned a user")
There's result:
Traceback (most recent call last):
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await self.prepare(ctx)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
await self._parse_arguments(ctx)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 697, in _parse_arguments
transformed = await self.transform(ctx, param)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 552, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 505, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 451, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "C:\Users\FreezeGames\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\converter.py", line 195, in convert
raise MemberNotFound(argument)
discord.ext.commands.errors.MemberNotFound: Member "467762820734386196" not found.
UPDATED: Using "member: discord.User", how do I write a condition if the user has a specific role?
Thanks in advance for any help you can offer, and let me know if you need any more information out of me.
A member converter is going to be useless if you want to get a user who is not on your server. Use the user converter instead:
async def ban(ctx, member: discord.User, *, reason=None):
await member.ban()
await ctx.send("You have banned a user")
async def ban(ctx, member: discord.Member = None, *, reason=None):
running = True
while running:
try:
await member.ban()
await ctx.send("You have banned a user")
except:
continue
else:
running = False
This could possibly work. I am not sure though.
I have a bot that sends dm pings to a user whos Id is given as a parameter in a command.
This is the command:.spam ID #_OF_PINGS
I run into the following error:
Ignoring exception in command spam:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 16, in spam
await ctx.send(f'Started pinging {user.name} {num} times.')
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'
Here is my code:
from discord.ext import commands
token = 'MyBotTokenHere'
prefix = '.'
client = commands.Bot(command_prefix=prefix)
client.remove_command("help")
#client.event
async def on_ready():
print('Ready')
#client.command()
async def spam(ctx, id1: int, num: int):
user = client.get_user(id1)
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
client.run(token)
It was working fine yesterday, but today, it broke down for some reason.
How do I fix it?
P.S. I hosted it on Heroku
You can use a converter for this:
#client.command()
async def spam(ctx, user: discord.User, num: int):
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
This way, Discord will automatically try to get the discord.User instance that corresponds to the id that you pass as an argument, and you can also #mention someone if you want to & it'll still work.
Also, starting from Discord 1.5.0, you now need to pass in intents when initializing a bot, which you clearly aren't doing yet. Update your Discord, and use the following to initialize your bot:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=prefix, intents=intents)
More info on the how's and why's of intents in the relevant API documentation. You'll also want to enable members on your bot's Privileged Intents page, which is explained in the linked API docs as well.
I'm trying to build a discord bot for reaction roles. To do this, I'm trying to use the on_reaction_add combined with fetch_message to check if the reaction added was to the message that the bot sent but I keep getting various error with fetch_message. Here is the code:
#bot.command()
async def createteams(ctx):
msg = await ctx.send("React to get into your teams")
await msg.add_reaction("1️⃣")
await msg.add_reaction("2️⃣")
await msg.add_reaction("3️⃣")
await msg.add_reaction("4️⃣")
await msg.add_reaction("5️⃣")
await msg.add_reaction("6️⃣")
await msg.add_reaction("7️⃣")
await msg.add_reaction("8️⃣")
#bot.event
async def on_reaction_add(reaction, user):
id = reaction.message.id
if await reaction.message.author.fetch_message(reaction.message.id) == "React to get into your teams":
print("Success")
This gives me the error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "hypixel.py", line 60, in on_reaction_add
fetch = await reaction.message.author.fetch_message(id)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/abc.py", line 955, in fetch_message
channel = await self._get_channel()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
ch = await self.create_dm()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 109, in general
return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
But when I go and copy the ID of the message I reacted to, its the same as the printed variable id but it still says Message Not Found
Thanks
I figured out the issue, you can't use fetch_message with user it needs to be channel so I changed my code to
await reaction.message.channel.fetch_message(id)
and it worked :)
I'm trying do a simple bot with subclass to handler the commands but i got this error
Here Are the TraceBack:
Ignoring exception in command teste:
Traceback (most recent call last):
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 721, in invoke
await self.prepare(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 685, in prepare
await self._parse_arguments(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 599, in _parse_arguments
transformed = await self.transform(ctx, param)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 445, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
And here are the code:
import discord
from discord.ext import commands
import inspect
class BotLibertarin(commands.Bot):
client = discord.Client()
#client.event
async def on_message(self,message):
print(f"message from {message.author} what he said {message.content}")
await self.process_commands(message)
class CommandsHandler(BotLibertarin):
def __init__(self):
super().__init__(command_prefix=".")
members = inspect.getmembers(self)
for name, member in members:
if isinstance(member,commands.Command):
if member.parent is None:
self.add_command(member)
#commands.command()
async def teste(self,ctx):
await ctx.channel.send("teste")
I really suggest that you spend time reading the docs.
I'd also remind you that it's against ToS to send messages without a specific command being issued (such as !commandname) [Even though no one follows ToS apparently].
Either way. Try this:
import discord
from discord.ext import commands
client = commands.Bot(command.prefix='!')
#client.event
async def on_message(message):
print(f"message from {message.author} what he said {message.content}")
await message.channel.send(message.content)
#client.command()
async def teste(ctx):
await ctx.channel.send("teste")