I want to check the time difference between when a command is executed and when the parameter specified in the command states it should end. I have an idea of how to do this, but if i merely stick a loop in my index.js which is the main file that I have the command group names stored will the loop continue to run without it stopping the bot from receiving input from people typing a command in a discord channel (like !flip)? How would I go about making it so it won't stop the other input if it does?
For this you will need to make use of async as otherwise node will wait for the loop to finish.
Try putting
<client>.on('message', async message => { /*code*/})
Related
I would like my bot to reply to a slash command by sending a message to a specified channel. However, every example I have seen only sends these messages through the client.on() or client.once() functions where they pass in the client as a parameter, then use client.channels.cache.get('channel_ID').
My execute(interaction) function for this command takes the interaction as a parameter instead. I do know I can pull the channel_id of the interaction from interaction.channel_id, but then it seems like I still have to get a handle on that channel through the client.
Can I pass in the client and the interaction when executing slash command functions? Is there another way to get a handle on the channel in order to send to it without using client?
All Discord.js classes have an internal client property, so for you all you need to access is interaction.client.
I am trying to make use of vscode's window.showInformationMessage method during a command that takes a screenshot of the current screen. After the screenshot has been taken, I use
await window.showInformationMessage(`File saved to ${path}`,"Open", "Copy Path");
Which gives the user the option to either open their saved image in a file explorer or copy the path to the clipboard. However, if the user does not do anything when the Information Message dialog pops up, then this call never resolves. On top of that, the user can run the screenshot command as many times as they would like, and each time the code execution would halt, waiting for the promise to resolve.
I have used two different methods to solve this:
Use a Promise.race with one parameter being a setTimeout promise
Use a .then at the end of the call to showInformationMessage, and remove the async.
QUESTION 1: what is happening under the hood here? How can I run this command many times if node runs on a single thread? Is another thread being created for each command call or is there a new instance of the extension being created each time I call this command?
QUESTION 2: What is a good way to see all of this? I have used the debugger but it hasn't helped much.
I have a working slash command, which is great. I can use the command with one input parameter eg /command string and it will execute the command.
However I am looking to set up a Discord bot that uses that command in a channel every 5 or so minutes. I can't seem to get the bot to use the command, any ideas on how to get it to work?
It just displays the string in the channel but the bot doesn't execute the command.
bot.on('messageCreate', async (msg) => {
if (msg.content === "!loop") {
interval = setInterval (function () {
msg.channel.send("/command string")
}, 3 * 1000);
}
})
Bots can only receive Application Command Interactions, not create them. This means your bot won't be able to run other bot's slash commands, click their buttons or use their dropdown menus.
If you have control over the other bot's code, though, you can set it up to listen for messages (not interactions) from your second bot, and run code accordingly.
But be aware: as MegaMix mentioned in their comment, if the bot you want to control isn't yours, you probably won't be able to do that, as it is a best practice to ignore messages from other bots to prevent abuse and infinite loops.
So I'm trying to have my bot restart ENTIRELY on encountering an error. The reason why I don't just let it sift through connection errors is because, whenever I encounter an internet issue, code starts repeating multiple times since the original node process hasn't been terminated, which technically I could fix but other connections to external apis stop working too. So ignoring fixing singular issues, I just want to restart entirely.
What I'm doing currently is using node child_process, with this function:
spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit"
});
process.exit();
I do know stdio inherit does nothing, since its exiting the parent process, but it doesn't really change anything to put it to ignore so i've just left it. Basically this works in theory, if I use a command to execute this, i can do it over and over and over and it will work fine, singular discord client, no repeats, it's up, i just can't monitor it since my original terminal is disconnected, and I can use a command to exit the current process so it's not stuck since I don't have a terminal to ctrl-c. But once put in practice, executing the function in bot.on("error") by disconnecting my internet seems to work, it ends the first process, but upon regaining internet there is no client connected.
My guess here: bot.on("error") will not be re-executed in the next process due to no discord client being made.
So I don't know if I'm making this too complicated or if I need to add a lot more. If this is the best way to do it then all I would need to solve is to wait until I have internet back and then make a new process or something like that. I'm not educated in fiddling with node so if any answers could be beginner friendly (mainly for node) i'd really appreciate it.
bot.on("error", (err) => {
process.exit(0)
});
Should work, it'll restart the bot when there's an error.
Unsure what you mean by
My guess here: bot.on("error") will not be re-executed in the next process due to no discord client being made.
As long as you bot it in the same code as your startup, it'll restart the bot.
If you use a batch-file to run your bot simply add :a before the node . and goto a at the end.
I am trying to make a discord bot. However, i am stuck on a part where i have to check every hour if a discord username is equal to the contents of an array. If it is, I have to make the bot remove a role. However, doing so would require using the discord.js module, which I don't know how to add into the code (I already have the bot set up in index.js).
I've tried to require discord.js-commando but it returns:
"Error: A client must be specified"
This is the place of the problem:
class checkDet extends commando.Command {
This is where I want to use the discord.js module:
var guild = bot.guilds.get("GUILD ID HERE");
for (let [k, v] in Object.entries(guild.members)) {
if (v.user.username == userName)
{
v.removeRoles(v.roles).then(console.log).catch(console.error);
}
}
Multiple solutions. Solution 1: if you're just using Discord.JS and not the whole Commando module, try using const Discord = require('discord.js'); at the top of that file and use
it as normal!
Another method would be to pass the whole commando module through. There's many ways to do it, however the easiest is probably to add let commando; at the top of the file, then create a module.exports.define function to set commando to the first argument. Then in the main bot file, run require('path/to/my/command.js').define(commando);.
Or even simpler, if all you need is a recursive check, why not put it in the main bot.js file, or even another file? Either invoke the file including the commando object, or just run a setInterval in the original file with the normal code.