Discord.js v13 voice channel data is not updating - javascript

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.

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 bot reply with slash command

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.

message.channel.members no longer contains all users in a text channel

I have a simple command that mentions a random user in a text channel. It worked perfectly for weeks, until today.
TL;DR:
channel.members is a map that used to contain all of the users of the text channel, but not anymore. Is there an error on my side or has something changed?
Before today, map would correctly contain all users that are in a text channel where message event happened. Now, for some reason, in this map there is an author and a bot itself + seemingly random people.
At first he seemed to also include people in a voice channel, and now nobody is in any of the voice channels and after the first message there was only me and a bot in the map, and when I asked someone to do a command, then he also has appeared in the map.
So that made me think that the channel property from message event's arg might have a map of people that messaged anything while bot was live, but how would that explain the people from the voice channel? Furthermore, before today, he would randomize anyone, even the people that have never posted a single message in any of the channels, ever.
Has Discord API changed recently? Because I cannot find any announcement or anything like that and I have no idea why it doesn't work anymore. I even checked out to few commits ranging from yesterday to couple days ago and it still behaves this way.
I double checked everything and I'm sure I have people that didn't say anything anywhere, and I see that bot has mentioned these people last time at 28 of October.
main.js
bot.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
require(`./commands/random_member`).execute(message, null);
});
random_member.js
module.exports = {
name: 'random_member',
usage: 'random_member',
description: "random_member",
execute(message, args) {
const member = message.channel.members.random();
message.channel.send(`${member}`);
}
}
Worthy Alpaca helped me with his comment.
"Firstly, you must manually enable the intents from the Discord Developer site. Go to applications, select your app, and find the "bot" tab on the sidebar. Then you can scroll down until you see this:"
I've enabled both of the options and my issues have disappeared.
Source:
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?

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.

discord.js - How to get the voice status of the bot in a specific server

In a program, i want to know if the bot is connected or disconnected in any voice channel on a specific server. I see the Voice Statuses on the wiki but i dont know how to use it.
You should try read the documentation a little bit more.
So, from client you have all the guilds that client is: client.guilds. Then from that you can filter the guilds, client.guilds.get('thisID'). And according to the documentation, with a guild you can get the voiceConnection from that guild with: guild.voiceConnection.
It will return null if the client is not on any voiceChannel.

Categories

Resources