#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
Related
I have this code:
#rob
#client.command()
#commands.cooldown(1,10800,commands.BucketType.user)
async def rob(ctx,member:discord.User = None):
if member == None:
em = discord.Embed(title='ERROR:', description='Please specify a member you want to rob!', color=0xff0000)
await ctx.send(embed = em)
return
await open_account(ctx.author)
await open_account(member)
bal = await update_bank(member)
if bal[0]<50:
em = discord.Embed(title='ERROR:', description="The member you try to rob doesn't have that much money!", color=0xff0000)
await ctx.send(embed = em)
return
if amount<0:
em = discord.Embed(title='ERROR:', description="Amount must be positive!", color=0xff0000)
await ctx.send(embed = em)
return
earnings = random.randrange(0, bal[100])
fine = random.randrange(0, bal[100])
em1 = discord.Embed(title='Succesfully robbed!', description=f"You robbed {earnings} coins!", color=0x00ff00)
em2 = discord.Embed(title='Caught!', description=f"You got caught! You paid a {fine} coins fine! ", color=0x00ff00)
yesorno = random.randrange(0, 1)
if yesorno == 1:
await ctx.send(embed = em1)
await update_bank(ctx.author,earnings)
await update_bank(member,-1*earnings)
else:
await ctx.send(embed = em2)
await update_bank(ctx.author,-1*fine)
This function's goal is to rob someone and take their money or give them a fine, but it doesn't work. It doesn't send em1 or em2, I don't know how to fix it. I tried many things, but it doesn't give an error!
What fixed it for me was changing the yesorno function. Changed it to:
yesorno = random.randrange(0, 2)
if yesorno == 1:
await ctx.send(embed = em1)
await update_bank(ctx.author,1*earnings)
await update_bank(member,-1*earnings)
else:
await ctx.send(embed = em2)
await update_bank(ctx.author,-1*fine)
await update_bank(member,1*fine)
So i have an embed system working in the below code:
#client.event
async def on_message(message):
if message.content.startswith('!emsay'):
embedVar = discord.Embed(title="Title", description="desc", color=0x00ff00)
await message.channel.send(embed=embedVar)
My problem is yes when i do !emsay it dose put the embed in but is there a way that after i put !emsay i can change the title and description without going into python to type out a whole new embed?
This is what i currently have:(im fine with this)Discord screenshot of embed I would like to make it so after i use the command !emsay i can enter a title and description through discord.
Thanks : )
sorry if iv overcomplicated this
Try this:
#client.event
async def on_message(message):
if message.content.startswith('!emsay'):
count = 0
def check(author):
def inner_check(message):
if message.author != author:
return False
return inner_check
while count < 2:
if (count == 0):
await message.channel.send("Write a title for embed")
title = await client.wait_for("message", check=check, timeout=30)
elif (count == 1):
await message.channel.send("Write a desc for embed")
desc = await client.wait_for("message", check=check, timeout=30)
count += 1
embedVar = discord.Embed(title = title.content, description = desc.content, color = 0x00ff00)
await message.channel.send(embed=embedVar)
If you don't write title and desc, you can get a TimeoutError and you can change timeout if you want.
How to use?
I am trying to make a discord giveaway reroll command.
The problem is that when the command is run it is not responding/rerolling the giveaway.
I've looked on multiple sites with the same command and all of them did not work/have a fix for this.
I also tried without the embeds to see if that was the problem with the code.
Below is the reroll command code -
(if the giveaway command code is needed I can provide it)
#client.command()
#commands.has_permissions(kick_members=True)
async def reroll(ctx, channel : discord.TextChannel, id_ : int):
try:
new_msg = await channel.fetch_message(id_)
except:
embed = discord.Embed(title="Command Error ⛔ - GameBot", description=f"**The Id Of A Channel Was Entered Incorrectly!** 🎉", color=0x992d22)
await ctx.send(embed=embed)
return
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(client.user))
winner = random.choice(users)
embed = discord.Embed(title="Giveaways 🎉 - GameBot", description=f"**Giveaway Has Been Rerolled!** \n \n**Winner -** \n`{winner.mention}`", color=0xe74c3c)
await ctx.send(embed=embed)
Below is the giveaway command code -
#client.command()
#commands.has_permissions(kick_members=True)
async def giveaway(ctx):
embed = discord.Embed(title="Giveaway Setup 🎉 - GameBot", description=f'**{ctx.author.mention} Giveaway Setup Is Now Starting... Please Answer These Questions Within 30 Seconds!**', color=0xe74c3c)
await ctx.send(embed=embed)
questions = ["**What Channel Should The Giveaway Be Hosted In?** `EX : #general` 🎉",
"**What Is The Duration Of The Giveaway?** `EX : S/M/H/D` 🎉",
"**What Is The Giveaway Prize?** `EX : Gift Card` 🎉"]
answers = []
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
for i in questions:
await ctx.send(i)
try:
msg = await client.wait_for('message', timeout=30.0, check=check)
except asyncio.TimeoutError:
embed = discord.Embed(title="Command Error ⛔ - GameBot", description=f"**Please Answer All Of The Questions In Time... Be Prepared!** 🎉", color=0x992d22)
await ctx.send(embed=embed)
else:
answers.append(msg.content)
try:
c_id = int(answers[0][2:-1])
except:
embed = discord.Embed(title="Command Error ⛔ - GameBot", description=f"**Please Provide A Valid Channel For Me To Host The Giveaway In!** `EX : #general` 🎉", color=0x992d22)
await ctx.send(embed=embed)
return
channel = client.get_channel(c_id)
time = convert(answers[1])
if time == -1:
embed = discord.Embed(title="Command Error ⛔ - GameBot", description=f"**The Time Constraint You Answered With Was Not A Valid Unit!** `EX : S/M/H/D` 🎉", color=0x992d22)
await ctx.send(embed=embed)
return
elif time == -2:
embed = discord.Embed(title="Command Error ⛔ - GameBot", description=f"**The Time Must Include An Integer!** `EX : 1S, 1M, 1H, 1D` 🎉", color=0x992d22)
await ctx.send(embed=embed)
return
prize = answers[2]
embed = discord.Embed(title="Giveaway Setup 🎉 - GameBot", description=f'**Giveaway Channel -** \n`{channel.mention}` \n**Duration -** \n`{answers[1]}`', color=0xe74c3c)
await ctx.send(embed=embed)
givembed = discord.Embed(title="Giveaways 🎉 - GameBot", description=f'**Giveaway Prize/Description -** \n`{prize}`', color=0x2ecc71)
givembed.add_field(name = "**Host -**", value=ctx.author.mention)
givembed.set_footer(text = f"Ending {answers[1]} From Now! 🎉")
my_msg = await channel.send(embed=givembed)
await my_msg.add_reaction("🎉")
await asyncio.sleep(time)
new_msg = await channel.fetch_message(my_msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(client.user))
winner = random.choice(users)
embed = discord.Embed(title="Giveaways 🎉 - GameBot", description=f"**Giveaway Has Ended!** \n \n**Winner -** \n`{winner.mention}` \n**Prize -** \n`{prize}`", color=0xe74c3c)
await ctx.send(embed=embed)
Almost everything about the code is correct. However I guess you are requesting the users in a wrong/not working way. You can try to request the users in another way, this seems to be the source where the error comes from.
Try out the following:
users = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user]
winner = random.choice(users)
The full code would be:
#client.command()
#commands.has_permissions(kick_members=True)
async def giveaway(ctx):
# Giveaway code
#client.command()
#commands.has_permissions(kick_members=True)
async def reroll(ctx, channel: discord.TextChannel, id_: int):
try:
new_msg = await channel.fetch_message(id_)
except:
embed = discord.Embed(title="Command Error ⛔ - GameBot",
description=f"**The Id Of A Channel Was Entered Incorrectly!** 🎉", color=0x992d22)
await ctx.send(embed=embed)
return
user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user]
winner = random.choice(user_list)
embed = discord.Embed(title="Giveaways 🎉 - GameBot",
description=f"**Giveaway Has Been Rerolled!** \n \n**Winner -** \n`{winner.mention}`",
color=0xe74c3c)
await ctx.send(embed=embed)
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
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}**!')