I've encountered a problem with client.wait_for().
while True:
msg = await client.wait_for('message', check=lambda message: message.author == ctx.author)
While running an infinite loop, when I type in the same command, there is another "instance" of that command running.
Is there a way to prevent the usage of commands during the client.wait_for() response?
I tried with startswith('prefix') but It's not working, the commands execute anyway.
To limit the usage of ONE command so that it can to allow a certain number of invocations at the same time, use the decorator max_concurrency.
You could set a variable commandsDisabled = True before the wait_for and set it to False after. Then check for this variable in every command
Related
I got the problem, that when I use the purge command in my Bot using discord.py rewriting method, it doesn't work. What I mean by that is that when I run the code, and then write 'clear' in the discord channel, it simply doesn't deletes the given amount of messages, and it also doesn't raises an error. I've also tried to put print('test') in the definition, but then it only prints test...
That is the Code I used to do this:
#client.command
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount)
the proper use of #client.command is #client.command(). See if that fixes it.
I'm using
#client.event
async def on_message(message):
to check for messages sent by users.
I'm also using
#client.command(aliases=['a'])
async def a(ctx):
to check if a command is being run. when i leave the on message in, the command part won't run. what do i do.
If you're using on_message event, it blocks your commands. In order to prevent this, you can use await client.process_commands(message). You just have to add this line of code in the last line of your on_message event.
Let's say I made an infinite command for my bot, Would there be any way to stop the loop at any time? I want to be able to stop it from the server, not in the actual code.
Example:
if(msg.content === "Just Monika"){
msg.channel.send('Just Monika')
}
})
Is there any way I can type something in chat, and it stops the command? thanks.
Making your bot respond to itself infinitely probably isn't a good idea. But just for learning, it's very possible to do what you wish.
You could make a different phrase (let's call it the stop command) set a boolean variable in your code to true. Then, whenever the looping command gets triggered by a user message or by one of its own, it should check if this boolean telling it to stop is true. If it is, it should set it to false and not send that message, else it should just send that message as per usual.
// The following should be defined in the outmost scope
let loopPhrase = "Just Monika";
let stopPhrase = "Stop Spamming The API";
let triggerStop = false;
// The following should be a part of the message event
if (msg.content === loopPhrase) {
if (!triggerStop) msg.channel.send(loopPhrase);
else triggerStop = false;
} else if (msg.content === stopPhrase) triggerStop = true;
So I created a program that allows you to have a variable that shows how many times a command has been executed, and here is what it looks like:
var timesRunned = 0
if(message.content.startsWith(`${prefix}command`)) {
timesRunned++
let myGuild = client.guilds.cache.get('Guild-ID')
let channel = myGuild.channels.cache.get('Voice-Channel-ID')
channel.setName('Commands Executed: ' + timesRunned)
.catch()
}
But after I run the command more than 3 Times, The voice channel does not even change no matter how many times I run the command, it just stays as "Commands Executed: 2"
Am I doing something wrong? This is in the main app javascript file. The bot itself has enough permissions.
Okay, here's the problem. It seems like you are putting the var timesRunned = 0 in the message event. This means that every time you run a command, this number is reset to 0, which means it forever stays at 1.
If you move the variable to outside the message event, it should work fine.
I will give you another tip, because by just moving it to outside the message event, the variable will clear every time it restarts.
What you can do is get the channel each time the command is run and see how many are on the channel already, then add 1. You would end up with something like this:
var channel = client.channels.cache.get('someID');
var numberExecuted = parseInt(channel.name.split(':').trim());
channel.setName(`Commands Executed: ${numberExecuted + 1}`);
Hope this helps.
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.