How do you detect voice channel streaming? (discord.js) - discord.js

Is there any event for when users stream directly in the voice channel?

client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
if (!newVoiceState) {return false};
if (newVoiceState.streaming) {
console.log(`${newVoiceState.member.user.tag} is streaming.`);
};
});
https://discord.js.org/#/docs/main/stable/class/VoiceState

Related

How to ignore same command after joining a Voice channel for bot discord.js

How to make a bot to ignore the same command after the bot joins a voice channel?
So if a user with permissions uses a command more than one time, the bot will just ignore the same commands because it has already joined the voice channel (the bot will ignore ?zipfm, but will not ignore others like ?disconnect or ?leave)
I tried making a cool down command, but still, if a user sends ?zipfm command more than one time bot starts to search dispatcher again after every command so we need to ignore that command after first time use.
client.on('message', async(message, user) =>{
if (message.content === '?zipfm'){
const role = message.guild.roles.cache.find((r) => r.name === 'ZIP FM 🎧🎶')
//--------------------------------------------------------------------------------------------------------
if (!role) return message.channel.send(`ZIP FM rolė nėra sukurta. Rašyk \`?sukurti\` norint sukurti ZIP FM rolę!`).then(msg => {
msg.delete({timeout:5000})
})
if (!message.member.roles.cache.has(role.id)) return message.channel.send(`Å i komanda yra leistina tik vartotojams turintiems ${role} rolÄ™.`).then(msg => {
msg.delete({timeout:5000})
})
if (message.channel.type == "dm") return;
if (message.member.voice.channel){
message.member.voice.channel.join().then(connection => {
connection.voice.setSelfDeaf(true);
message.channel.send("Paleidžiama ZIP FM 🎶").then(msg => {
msg.delete({timeout:10000})
})
const dispatcher = connection.play('https://transliacija.zipfm.lt/zipfm128.mp3',{filter: "audioonly"})
})
}
}
After checking if the message content is equal to ?zipfm, you can simply use the GuildMember#voice() function in order to determine whether or not the client is currently inside of a voice channel.
You can simply do that using:
if (message.guild.me.voice) return message.channel.send('I'm already inside of a voice channel!');

How to get the channelid or name of a newly created channel

I have a command with my discord bot to make a channel, but I'm trying to figure out how to get the id or name of the channel right after its made.
Line of code that makes the channel:
message.guild.channels.create('ticket ' + message.member.displayName, { parent: '744477882730020965' });
The reason is that since displayname can have characters not possible in a channel name and discord will just automatically remove those characters there's no actual way to predict what the channel name will be in some cases. There's probably a simple solution I'm missing, and thanks in advance for any help.
The GuildChannelManager#create method returns a Promise with the channel that was just created. You can use Promise.then() to get the channel.
Guild.channels.create(`ticket-${message.member.displayName}`, {
parent: "744477882730020965"
}).then(channel => {
message.channel.send(`I've created the channel ${channel.name}!`);
}).catch(error => {
console.error(error);
message.channel.send(`Couldn't create the channel. Check the console for the error.`);
});
If you are creating the channel in an async function, you can use await to avoid .then() for readability.
const Channel = await Guild.channels.create(`ticket-${message.member.displayName}`, {
parent: "744477882730020965"
}).catch(error => {
console.error(error);
message.channel.send(`Couldn't create the channel. Check the console for the error.`);
});
What do you need the ID for? You can use a then function to do whatever to edit the channel.
Try this?
let role = message.guild.roles.find("name", "#everyone");
message.guild.channels.create('ticket ' + message.member.displayName, { parent: '744477882730020965' }).then(c => {
c.overwritePermissions(role, {
SEND_MESSAGES: false,
READ_MESSAGES: false
});
c.overwritePermissions(message.author, {
SEND_MESSAGES: true,
READ_MESSAGES: true
});
message.reply(`Application created!`)
c.send(`Wait for support staff`)
}).catch(console.error);

How to send a message to every user that is in same guild as bot?

So basically I want to make bot able to send a message to every user that is in the same guild as a bot, I want to make it with a timeout, so I don't abuse Discord API.
Sending DM's using setTimeout would take forever so I filtered guilds with a channel named auto-partnership
setTimeout(() => {
let channel = client.channels.cache.filter(channel => channel.name.toLowerCase() === "auto-partnership")
channel.forEach(channel => {
channel.send( "test")
})
}, 5000);
A bot is sending test message to every guild with a channel named auto-partnership

How do you make some bot commands only work in some discord channels

This is my first time using js. I need to ban a certain word in a certain channel. I don't want the message to be deleted if it is not in the specific channel.
For Example:
I want to ban the word "goodbye" in the #greeting channel
BUT
I don't want to ban the word "goodbye" In the #farewell channel
How would I go about this?
Thanks.
By the way I wanted to use example code, but none of it made any sense.
I wrote a simple example for you on how to achieve this:
const Discord = require("discord.js");
const Client = new Discord.Client();
const ForbiddenWords = {
"ChannelID": ["goodbaye", "bye"] // Greeting channel
};
Client.on("ready", () => {
console.log(`${Client.user.tag} is ready!`);
});
Client.on("message", (message) => { // Fired when the bot detects a new message.
if (message.author.bot) {return false}; // Checking if the message author is a bot.
if (ForbiddenWords[message.channel.id]) { // Checking if the current channel has any forbidden words in the ForbiddenWords Object
if (ForbiddenWords[message.channel.id].some(word => message.content.toString().toLowerCase().includes(word))) { // Checking if the message contains any forbidden words
message.delete(); // Deleting the message.
};
};
});
Client.login(process.env.DISCORD_AUTH_TOKEN);

Discord.js support channel

I want to create an if variable which checks if a user joins a certain channel, if they do then I'll keep doing my program.
What I have tried so far is different things like:
client.on('channel', async channel => {
if (channel.member.join(channel => channel.id === `672920107457970177`)) {
working.send("did it work??")
}
})
But I am pretty sure that my whole script is wrong; I only started using discord.js a few weeks ago.
You can use Client's voiceStateUpdate event. It will return 2 GuildMember values. The old one and the updated one.
Client.on("voiceStateUpdate", (oldMember, newMember) => {
if (!oldMember.voiceChannel && newMember.voiceChannel) {
console.log("oldMember channel doesn't exist. newMember channel exist. User joined a voice channel.")
} else if (!newMember.voiceChannel) {
console.log("newMember voice channel doesn't exist. User left a voice channel.")
}
})
You need to do these checks because voiceStatusUpdate is fired whenever the voice status updates (e.g. defean, mute, channel move etc.)
To check if the user joined a certain channel, just check the ID:
Client.on("voiceStateUpdate", (oldMember, newMember) => {
if (!oldMember.voiceChannel && newMember.voiceChannel && newMember.voiceChannelID == "CHANNEL ID") {
console.log(`User joined ${newMember.voiceChannel.name}!`)
}
})

Resources