Discord.js bot reply with slash command - javascript

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.

Related

Discord - Automatically extract a message immediately after it appears in a channel?

I am yet to code this but actually struggling to understand how to do this
There is a private server with paid membership. I am a member. There are many channels on that server. I am particularly interested in a specific channel. Once someone posts a message in that channel, I want to immediately extract that message from a different program automatically and then process it in downstream systems. How to do that?
I know I'm a bit late to this question, but I'm going to tell you the secrets the Discord elites don't want you to know: This is completely doable, and many people do it. It's called a "self-bot".
Disclaimer: As the other answer stated, this is against ToS. You probably won't be caught, but I'd keep this on the down-low. Definitely don't go around telling this to everyone you meet.
Ok, now here's the actual coding part:
Discord.js, the most common library for discord bots in JavaScript, no longer supports self-bots, so you'll need to use an older version of discord.js. The official old versions have some unresolved bugs when used with modern discord, so I like to go with discord.js.v11.patch. Using this patched package will fix any weird bugs you get. Note: this is still discord.js v11, so if you need documentation, be sure to look at the v11 docs.
Ok, so after you run npm install discord.js.v11.patch (or npm install -g discord.js.v11.patch if you want to install it globally), you'll need to start writing code. Everything is basically the same as any old discord.js bot, but this is v11 so some stuff might be different. Here's some code to get you started. It should do everything you want it to do:
const discord = require('discord.js.v11.patch');
const client = new discord.Client();
const USER_TOKEN = 'XXXXXXXXXXXXXX'; // change this to your token
const CHANNEL_ID = 'XXXXXXXXXXXXXX'; // change this to the chanel you want to listen to.
client.on('ready', () => {
console.log('bot is running');
});
client.on('message', msg => {
if (msg.channel.id != CHANNEL_ID) return;
const message_text = msg.content;
console.log(message_text); // just an example
// send message_text somewhere to process it.
});
client.login(USER_TOKEN);
Now, all you need to do is change USER_TOKEN to your discord token and CHANNEL_ID to the ID of the channel you want to listen to.
To get your token, I recommend using this gist. It is safe and minimal. If you are worried about accessing your token, don't be. As long as you don't give it to anyone else you're fine.
To get the channel ID, simply turn on developer mode, then right-click on the channel you want to listen to. You should see a Copy ID menu button at the bottom of the context menu.
You can only do this if you have permissions to add an actual bot to that server, or have a friend that can do this for you. Using your own account as a bot would be possible, but is against discord ToS and would get your account banned.

Discord.js v13 voice channel data is not updating

My Slash commands are registered and they come up in discord.
Simplified version of my usage:
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
console.log(interaction.member.voice.channel);
interaction.reply('done');
});
When running this it will always give the ouput from when i started the bot. For example if I join a channel and then start the bot, it gives the correct output, but if i then leave the channel it gives me the same output. If I'm not in a channel when I start the bot, the output correctly is null but if i join a channel it will again still be null until i restart the bot.
Has anybody had the same problem / is my thinking wrong or does someone know a fix?
This is not a problem with the slash commands functionality. You have to add the GUILD_VOICE_STATES intent to your client. This lets it detect updates to voice channels such as when you leave or join.

Using node.js and the discord.js library to message users when I run a command, but it only starts to work after the targeted user runs the command

Like I said in the title, I'm trying to create a Discord bot that messages a user upon me running a command. It only works, however, after the targeted user runs the command themselves. After they run the command. I can run it and it messages them. If I restart the bot, the targeted user needs to run the command for it to work again. Why is this happening? I assume its possible to make it so they don't need to run the command first.
The code I am using is this:
if (cmd === "cmd1"){
bot.users.get("<user1ID>").send("Message");
}
if (cmd === "cmd2"){
bot.users.get("<user2ID").send("<Message>");
}
If I try to run, say cmd1, before user1 runs cmd1, it will throw an error in the console saying "TypeError: Cannot read property 'send' of undefined." The bot and users are in a mutual server. It seems to me like the issue is that the users are not being cached until the user themselves runs the command. I would like it to be so I can run the command to message people without them having to do so first. The purpose is to notify a few people of a pre configured message.
Because of Discord.js V12, you need to add a cache when fetching members by ids
bot.users.cache.get("<user1ID>").send("Message");

Discord bot ignore all webhooks?

I have a discord bot that uses a leveling system. It listens to message.author.id, logs the latest message, gives the user XP and at certain levels, users are awarded a new role. Have been working fine for months.
Recently I added a few(10 to be exact) webhooks to the server to enable users to send in bug reports.
Problem is that my bot is reading those webhooks messages as well, and webhooks don't have an author ID, hence, bot crash every time a webhook is posting something.
I know we can except certain webhooks like this if (message.webhookID != 'X') return;
but its kinda inconvenient since I might add or delete webhooks in the future. Is there a way to make my bot ignore all webhooks similar to how it can ignore other bots?
EDIT
This is what I did,
client.on('message', (message) => {
//Check if its a webhook.
if (message.webhookID) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
if (message.guild.id != '335008833267040256')return;{
....
Yes, you can do if (message.webhookID) return;, that should return if the message is sent from any webhook.

Checking time using a discord bot (Javascript)

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*/})

Categories

Resources