Store channel id with command - node.js

I'm working on a discord.js v13 bot and I want to make a commmand that stores the channel id the command sent in then, A command that works only the channels stored .
I want an example of the code to do this :)
I didn't try bec I can't realize how to do it :)

It's pretty easy when you know how to do it actually. You can listen on messages in different channels with:
client.on("message", (message) => {
console.log(`Channel-ID: ${message.channel.id}`);
});
See the Docs: Message
I don't know if it works in v13 but if not try this:
client.on("message", (message) => {
console.log(`Channel-ID: ${message.channelId}`);
});
To react when a command is used:
client.on("message", (message) => {
if(message.startsWith("!channel")){
console.log(`Channel-ID: ${message.channelId}`);
}
});

Related

Edit variables with command discord.js v13

I'm working on a discord.js v13 bot, and I want to make a command that add the channel Id where the command executed in an existing variable and, another command that works only with the channels in the variable
This is the variable
const ch = ["1068584484473143386","1068570010756337705","","","",]
The command that works with the channels stored
client.on("message", async (message) => {
if(message.author.bot) return;
if(ch.includes(message.channel.id)) {
const prompt = message.content
message.channel.sendTyping(60)
const answer = await ask(prompt);
message.channel.send(answer);
}});
Edit : I used array.push() in an command but it didn't work it didn't do anything
client.on('messageCreate', message => {
if (message.content === '..auto-on') {
if(!message.member.permissions.has("ADMINISTRATOR")) {
message.channel.reply("**You don't have permissions**")};
ch.push(`${message.channel.id}`)
message.channel.reply(`**Added ${message.channel} to auto-reply**`);
}});
Edit 2 : it worked but it get reset after restarting
If I understood you correctly you want to insert a new channel Id to an existing array, you can do that using the Array.push() method.
Example:
const arrayofItems = ["Flour", "Milk", "Sugar"]
console.log(arrayofItems) // ["Flour", "Milk", "Sugar"]
arrayofItems.push("Coffee")
console.log(arrayofItems) // ["Flour", "Milk", "Sugar", "Coffee"]
Make sure to learn the basics of JS before starting out with libraries such as Discord.JS.

If a message is from 3 specific users , ignore them

I want to know is it possible to stop specific users from using the bot, e.g. user b can not use any commands if they are in the blacklist of the config.json . thank you
Here is something you could use
const bannedUsers = ['userid1', 'userid2', 'userid3'];
client.on('message', msg => {
if(bannedUsers.includes(msg.author.id)) return;
//command execution code here
})
You will have to put this in all your client.on('message') listeners.
You don't show any code, but you could try something like this where you process commands..
//when bot receives a message
if (message.author.id === 'blacklisted ids'){
return;
} else {
//process commands
}

Discord Bot Server Muting

I'm trying to code a discord bot that server mutes people when i do ".mute #person"
What's wrong with my code?
bot.on('message', msg=>{
let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
if(msg.content === ".mute"){
person.setDeaf
}
})
It says "message not defined"
bot.on('message', msg => {...} waits for the event message then passes that information as the variable msg for the function provided.
Inside your function, you refer to message.guild.member but message is undefined (you passed your event information as msg, not message). Change your function parameter to message, so it looks like this:
bot.on('message', message => {...})
That would fix the error you're getting, but I'm not sure that function will actually server mute a user. I think what you want is:
client.on('message', async message => {
if (message.content.startsWith(".mute")) {
let person = message.guild.member(message.mentions.users.first());
await person.edit({mute: true});
}
});
Notice I use message as the parameter for the function, but I put async in front of it because I'm going to be using an asynchronous function. .edit() takes a dictionary of data, see the linked documentation. It is awaited because it is done asynchronously: you call it, then you wait for a response from the server before continuing (to make sure everything happened as expected).
Tested myself, works like a charm.

Discord.js Music bot "TypeError" when playing audio with dispatcher

I'm new to Discord.js and I'm trying to have the bot join a voice channel and play an audio file on my computer. I have been following this guide: https://discord.js.org/#/docs/main/stable/topics/voice . Here is the Index.js page:
Colesbot.on('message', message=>{
if (message.content === '/join') {
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voiceChannel) {
message.member.voiceChannel.join().then(connection => {
message.reply('I have successfully connected to the channel!');
// To play a file, we need to give an absolute path to it
const dispatcher = connection.playFile('C:\Users\bobal\Documents\GitHub\Spotify-Playlist-Discord-bot\Assets\Glory.mp3');
dispatcher.on('end', () => {
// The song has finished
console.log('Finished playing!');
});
dispatcher.on('error', e => {
// Catch any errors that may arise
console.log(e);
});
dispatcher.setVolume(0.5); // Set the volume to 50%
}).catch(console.log);
} else {
message.reply('You need to join a voice channel first!');
}
}
});
exports.run = (client, message, args) => {
let user = message.mentions.users.first || message.author;
}
FFMPEG is installed and I have set the environment path for it. When I type FFMPEG in the command line I get the proper response.
Some have said I need to install the ffmpeg binaries but when I run npm install ffmpeg-binaries I get an error message that is here
So then I tried installing an older version and I'm now using ffmpeg-binaries#3.2.2-3 but when I type /join I get the error
[ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object
Firstly you need to reset your token, you should never post it online as others can use it to access and control your bot
here is a good answer to your question
I think that you need to install FFMPEG and have the environment path set for it, however, can you provide more information, console logs, behaviour etc

Unresolved method in WebStorm (discord.js, code works)

I'm wrapping my head around this. Autocompletion does not work .then(r => { HERE }) either.
Kinda starting out with this and would be way easier if it just works (works outside of the promise).
Code runs without any problems as well. delete method is recognized as well but not at that part.
I have this problem in a bigger project as well and it gets me confused.
Trying to find something on the web for a few hours, but couldn't find anything that helps me out. Hope I wasn't blind at some point :P
Whole test source:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something').then(r => r.delete(5000));
}
});
Problem:
If you need to delete command triget message you can use
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something')
message.delete(1000)
.catch(console.error)
}
});
If you use need to delete response message after some time you code must work, but you can try use reply method.
client.on("message", message => {
if (message.content === 'test'){
message.reply('somethink')
.then(msg => {
msg.delete(10000)
})
.catch(console.error);
}
});
Maybe problem in you discord.js version? In v.12 version you need use
msg.delete({ timeout: 10000 })
Just means webstorm can't discern what functions the object will have after the promise resolves. This is because the message create action in discord.js has multiple return types. So it's possible for the message object not to be passed into r, for instance in the event that the message failed to send, possibly by trying to send a message to a channel without the proper permissions.
If you add a check to confirm that r is the same type as message before trying to call .delete() I believe the warning will go away.
You can observe the potential error this highlight is warning you of by removing the bots permission to send messages in a channel, then by sending "test" to that same channel.
Having had the error recently and having found a solution, if for example you want a text channel, by doing a get you can have a text or a voice . so check if the channel instance TextChannel (for my example)
let channel = client.guilds.cache.get(process.env.GUILD_ID).channels.cache.get(config.status_channel);
if (channel instanceof TextChannel) {
await channel.messages.fetch()
}

Resources