Im having this issue and i still cant find a solution this is the code
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
await payload.member.add_roles(commands.reaction_role)
#commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.remove_roles(commands.reaction_role)
#commands.command()
async def set_reaction_message(self, ctx, message_id=None, role_id=None):
for channel in ctx.guild.channels:
try:
commands.reaction_message = await channel.fetch_message(int(message_id))
break
except:
pass
problem: File "c:\Users\MY-NAME\Desktop\Overige en school\Discord Bot\cogs\reaction.py", line 17, in on_raw_reaction_add
await payload.member.add_roles(commands.reaction_role)
AttributeError: 'RawReactionActionEvent' object has no attribute 'member'
can someone please help me this is been bothering me now for 2days and i can't even find a solution
I know according to the documentation discord.RawReactionActionEvent payload has the object member. But why don't you make it easier for yourself and get the member in the same way as in your on_raw_reaction_remove event? The following should work.
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.add_roles(commands.reaction_role)
#commands.Cog.listener()
async def on_raw_reaction_remove(self, payload):
if payload.message_id == commands.reaction_message.id and commands.reaction_role != None:
guild = self.client.get_guild(payload.guild_id)
member = guild.get_member(payload.user_id)
await member.remove_roles(commands.reaction_role)
#commands.command()
async def set_reaction_message(self, ctx, message_id=None, role_id=None):
for channel in ctx.guild.channels:
try:
commands.reaction_message = await channel.fetch_message(int(message_id))
break
except:
pass
Related
Not totally sure if I'm doing it correctly but here is the code:
dcoin = ""
#client.command()
async def ddraw(ctx, dcoin1):
def check(m):
return m.author.id == ddraw
await ctx.send(dcoin1)
#client.command()
async def matches(ctx):
coin1 = "5"
if coin1 == dcoin1:
await ctx.send("Match")
else:
await ctx.send("Not a Match")
To my understanding, "dcoin1" is set to whatever is typed in the ddraw command which then changes the global variable of "dcoin1". Therefore it can check if it matches with the match command. (Used 5 as an example)
No, dcoin1 in async def ddraw(ctx, dcoin1): is an argument. It exists only in your function.
You can write this:
dcoin = ""
#client.command()
async def ddraw(ctx, dcoin1):
global dcoin
dcoin = dcoin1
await ctx.send(dcoin1)
#client.command()
async def matches(ctx):
coin1 = "5"
if coin1 == dcoin:
await ctx.send("Match")
else:
await ctx.send("Not a Match")
In this code dcoin is a global variable and users can modify it using ddraw command.
I recently made a snipe command for my discord bot but I have one problem. Snipe command snipes a message from other channels when I for example want to snipe a message in general. Here is my code:
#bot.event
async def on_message_delete(message):
if message.attachments:
bob = message.attachments[0]
bot.sniped_messages[message.guild.id] = (bob.proxy_url, message.content, message.author, message.channel.name, message.created_at)
else:
bot.sniped_messages[message.guild.id] = (message.content,message.author, message.channel.name, message.created_at)
#bot.command(aliases=['s'])
async def snipe(ctx):
try:
bob_proxy_url, contents,author, channel_name, time = bot.sniped_messages[ctx.guild.id]
except:
contents,author, channel_name, time = bot.sniped_messages[ctx.guild.id]
try:
embed = discord.Embed(description=contents , color=discord.Color.purple(), timestamp=time)
embed.set_image(url=bob_proxy_url)
embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url)
embed.set_footer(text=f"Deleted in : #{channel_name}")
await ctx.channel.send(embed=embed)
except:
embed = discord.Embed(description=contents , color=discord.Color.purple(), timestamp=time)
embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url)
embed.set_footer(text=f"Deleted in : #{channel_name}")
await ctx.channel.send(embed=embed)
snipe_message_author = {}
snipe_message_content = {}
#client.event
async def on_message_delete(message):
snipe_message_author[message.channel.id] = message.author
snipe_message_content[message.channel.id] = message.content
#client.command(name = 'snipe')
async def snipe(ctx):
channel = ctx.channel
try:
em = discord.Embed(description = f"`{snipe_message_content[channel.id]}`\nMessage sent by {snipe_message_author[channel.id]}!", color = 0x00c230)
em.set_author(name = f"Last deleted message in #{channel.name}")
em.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
em.set_footer(text = f"Snipe requested by {ctx.message.author}")
await ctx.send(embed = em)
except:
embed = discord.Embed(colour = 0x00c230)
embed.set_author(name=f"There are no deleted messages in #{ctx.channel}!")
embed.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
embed.set_footer(text=f"Snipe requested by {ctx.message.author}")
await ctx.channel.send(embed=embed)
Thats what I use and it works properly.
Since I added [message.channel.id] to the variables it only checks for snipes in that channel. This is how it looks like in Discord
#commands.command()
async def gtn(self, ctx):
for guess in range(0, 5):
await ctx.send('guess')
number = random.randint(1,10)
response = await self.client.wait_for('message')
guess = int(response.content)
if guess == number:
await ctx.send('correct')
So this is my code, I want multiple users to guess the number and have unlimited chances but I'm not too sure how to do so.
You can use a while loop
#commands.command()
async def gtn(self, ctx):
while True:
await ctx.send('guess')
number = random.randint(1,10)
response = await self.client.wait_for('message')
guess = int(response.content)
if guess == number:
await ctx.send('correct')
break
Thats a test code where im trying to work in classes. Even tho in commands it displayes ctx as required arg instead and it works only if i put it before self.
class Quiz:
def __init__(self, question, answer):
self.question = question
self.answer = answer
#bot.command(pass_context=True)
async def quiz(self, ctx):
fido = Quiz("Starter mass?", "yeah")
nope = Quiz("WHAT?", "lel")
await ctx.send(fido.question)
def check(m):
return m.content == 'heh'
msg = await bot.wait_for('message', check=check)
await ctx.send('Right {.author}!'.format(msg))
Error:
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
How can I fix this?
As crazy as it sounds, remove the self argument from your method. The commands decorator will take care of this. (You can also use a command cog)
class Quiz:
def __init__(self, question, answer):
self.question = question
self.answer = answer
#bot.command(pass_context=True)
async def quiz(ctx):
fido = Quiz("Starter mass?", "yeah")
nope = Quiz("WHAT?", "lel")
await ctx.send(fido.question)
def check(m):
return m.content == 'heh'
msg = await bot.wait_for('message', check=check)
await ctx.send('Right {.author}!'.format(msg))
Also your check function doesn't work correctly. It always uses the fido question and says you are correct only after responding with 'heh'.
#bot.command(pass_context=True)
async def quiz(ctx):
fido = Quiz("Starter mass?", "yeah")
nope = Quiz("WHAT?", "lel")
question = random.choice((fido, nope))
await ctx.send(question.question)
check = lambda m: m.content.lower() == question.answer
msg = await bot.wait_for('message', check=check)
await ctx.send(f'Right **{msg.author.name}**!')
I'm setting up a discord bot, and I want to have the bot send a confirmation or error message after the commands "ban" and "kick", can anyone help?
I have tried creating another separate command with the same arguments, except just having it send the expected message.
#client.event
async def on_ready():
print('Bot is ready!')
#client.event
async def on_member_join(ctx, member):
print(f'{member} has joined {ctx.guild.name}.')
#client.event
async def on_member_remove(ctx, member):
print(f'{member} has left {ctx.guild.name}.')
#client.event
async def on_member_join(member):
role = discord.utils.get(member.server.roles, name="Member")
await client.add_roles(member, role)
#client.command()
async def ping(ctx):
await ctx.send(f'Pong! :ping_pong: **{round(client.latency * 1000)}ms** ')
#client.command()
async def purge(ctx, amount):
await ctx.channel.purge(limit=amount)
await ctx.send(f'{member.display_name} has been kicked.')
#client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f'{member.display_name} has been kicked.'')
#client.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f'The Ban Hammer has Spoken! {member.display_name} has been banned!')
#client.command()
async def pardon(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.banned_users
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned {member.display_name}')
In actuality, I think it would be great, because they clash with each other and probably will send a message, but it went wrong.
At the end of the kick command delete that last '. And also do {member.name} or {member}