How do I implement a wait_for function into my command? - discord

I am trying to make a bot that will pick a word from a text file and then scramble it and then the user unscrambles the word and types it. But I do not know how to implement the wait_for function into the command.
#client.command()
async def start(ctx):
await ctx.send('Time for chaos')
Unscrambled_word = random.choice(list(open('C:\\Users\\user\\Desktop\\Discord Bots\\Fake Speedjar\\words.txt')))

I suggest reading the documentation here
It comes with a good simple example.
There is another library called dpytools that has a helper (wait_for_author) for this kind of simple cases you can check it here
Basically the wait_for method will wait for something and then return it to you. In this case a "message".
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
message = await client.wait_for('message', check=check)
The check function takes only the awaited object(s) and returns a bool indicating if the message Is what you're expecting.
Please check the documentation for further information.

Related

Toggling a server limited command

#f spam
async def on_message(message):
if message.author.bot:
return
elif message.content.lower() == 'f':
await message.channel.send('f')
await bot.process_commands(message)
this is my current code for an f spam or sends an f each time a user does.
I was wanting to make this command toggleable and also server/guild limited if possible.
for example someone says !fspam and it gets toggled and switched off and when done the same again it gets turned on. OR it could be !fspam on/ !fspam off
You can use Command.enable, where you can use command.update to. This will raise the DisabledCommand error.
Also please dont just copy and paste the code, try understanding what I did.This will disable it for all the guilds to. If you want it per server then you will need to use a database.
For example:
#client.command()
async def enable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=True)
await ctx.send(f"{command} enabled!")
#client.command()
async def disable(ctx,*,command):
command = client.get_command(command)
command.update(enabled=False)
await ctx.send(f"{command} disabled!")

how to add reaction to a specifc user?

I've been working on this piece of code:
#client.event
async def lol(message,ctx):
if ctx.author.id == <user_id>:
await message.add_reaction('❤️')
else:
return
I am pretty sure that it is developed correctly, yet I am not getting the desired results.
I am pretty sure that if you check the console, an HTTP error would have been encountered. That's because you directly pasted the emoji in the function parameter. You have to add its unicode value. In your case:
await message.add_reaction(u'\u2764')
You are using an event decorator rather than a command, unless you were trying to detect if the user reacted, but its still completely off. What it looks like, the command that is called will send a message and the bot will react to it.
#client.command()
async def lol(ctx):
message = await ctx.send(f"{ctx.author.mention}, lol")
await message.add_reaction('😂')
If you then want to detect if the user had reacted to the emoji, you can use a event, but an on_reaction_add:
#client.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '😂':
await user.send("Lol")

Member Count Channel discord.py

The program should work in such a way that a channel named "members" will display the number of members on the server, but the program does not give errors and does not work itself.
Thanks in advance!
async def on_member_join(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
#bot.event
async def on_member_remove(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
I am not really sure if you have it in your program but just to be sure, define what is the attribute named "channel" so it knows what to edit, you can use get_channel to do it and put the channels ID afterward inside it ( https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_channel#discord.Client.get_channel)
Maybe just try and use it the old way and use name = "Учатники: " + str(guild.member_count) (The member_count gives you an output of int so you may need to turn it into a string before displaying it.
(I have not tested anything and this answer is based on experience and reading documents and also you might want to take a look at https://discordpy.readthedocs.io/en/latest/api.html?highlight=member_count#discord.Guild.member_count)

Wait for Message on_member_join using checks (discord.py) | SOLVED

I want to wait for a private message that was sent only by the member. I am trying to use the check parameter for the client.wait_for() method but I can not seem to get it. I tried putting the check as the response.author but will not work since I just assigned response. Also I am executing this when a member joins using on_member_join which takes in a member argument (like this async def on_member_join(member). Anyone know how to properly do this? Any answers would be greatly appreciated!
Here is the code for this:
await member.send('What is your student number? ')
response = await client.wait_for('message')
student_number = response.content
When adding a check function to wait_for, that check function will get the received message as an argument. This means that it will check every single message the bot gets, pass all of them to your check function, and wait until one of them makes the check return True.
#client.event()
async def on_member_join(member):
def check(message):
return message.author.id == member.id and message.guild is None
await member.send('What is your student number?')
response = await client.wait_for('message', check=check)
student_number = response.content
Note how my example also makes sure the guild is None, as you explicitly mentioned waiting for a private message.

Is there a way to pm an author and use the wait_for command to capture the value they type in?

I'm writing a discord bot using python to log options trade. What I'm looking to do with my bot is when a user type execute a command, !opentrade, the bot would private message the user with a series of questions. The bot would then capture the answer in a series of variables and use that to process the answer. Is this possible or does it have to be in the channel where the command is called. Additionally, is it by default multithread, meaning 2 or 3 people can call that command at the same time and it would fud up the answer
I have the following thus far and it isn't working:
async def opentrade(ctx):
def check(author):
def inner_check(message):
if message.author != author:
return False
try:
int(message.content)
return True
except ValueError:
return False
return inner_check
try:
await ctx.author.send('Enter the underlying: ')
underlying = await client.wait_for('message', check=check(ctx.author), timeout=30)
print (underlying)
except Exception as err:
await ctx.channel.send("Something went wrong: {}".format(err))
thanks
What I'm looking to do with my bot is when a user type execute a command, !opentrade, the bot would private message the user with a series of questions. The bot would then capture the answer in a series of variables and use that to process the answer. Is this possible or does it have to be in the channel where the command is called.
Possible. You need to put the whole logic into the coroutine that executes the command.
Additionally, is it by default multithread, meaning 2 or 3 people can call that command at the same time and it would fud up the answer
If you implement the checks properly it won't fud the answer. In your code you already implemented the logic that checks for the author so noone else can change it def check(author).
When executing the commands multiple times by different users it will also not fud the answer since the threads are independent.
You should extend this check so it checks for a message in a DM channel. The way your check is written now the caller can DM the bot or respond in a guild text channel.
I'm not sure what's saved in the ctx variable but I'm certain it has a message attribute.
Try await ctx.message.author.send(). If you are running into an error provide the error log.

Resources