Discord.js moving person to random VoiceChannel - discord.js

I'm making a discord bot that will has a feature to move user's to a random voice channel if they want. I searched the internet 3 hours straight and checked the whole documentation. But still can't find anything.
note : I know this bot idea looks useless. But that's what I need.
code :
let voiceChannels = message.guild.filter(g => **idk how to check if it's vc** );
that's what I just found in 3 hours.

You'd need to access the guild's channels and then choose a random channel of type voice.
So in your case, it'd be:
let voiceChannel = message.guild.channels.cache.filter((channel) => channel.type === "voice").random();
if (!message.member.voice.channel) return message.reply("User is not in a voice channel");
await message.member.voice.setChannel(voiceChannel);

Related

Discord.js - Check if user is muted / mute change

I am trying to create a bot that server-mutes everyone if a specific person mutes themself.
I can get the user ID or other details via message.member.
Is there any way to find out if that user is muted or even if the person is in a voice channel?
This is what I found so far:
client.on('voiceStateUpdate', (oldmember, newmember) => {
let oldvoice = oldmember.voiceChannel;
let newvoice = newmember.voiceChannel;
if (oldvoice && newvoice && oldvoice.id != newvoice.id)
var channelStatus = 'Moved'
console.log(oldvoice)
})
But I don't understand it and it just returns undefined.
Edit: Found a way easier solution:
msg.member.voice.selfMute
Old one:
client.channels.cache.get(msg.member.voice.channel.id).guild.voiceStates.cache.get(msg.author.id).selfMute
let's break it down:
client.channels.cache gives you of all text and voice channels in your discord server
get(msg.member.voice.channel.id) gives you the id of the voice channel, the user that wrote the bot command (considering, you have stored the message in a variable e.g.: msg)
.guild.voiceStates.cache gives you a list of everyone in the channel
.get(msg.author.id) takes the user that wrote the last message
.selfMute returns the mute state
btw there are other pretty useful things too like selfDeafen, streaming and more

Allowing links only in a specific channel

I was wondering if any of you guys is able to help me out here, i'd like to make a channel to be links-only, meaning if you try to type or send a message there it will get deleted by the bot saying something like "ERROR! this channel is for links only" just like when you do a filter for links to be deleted. Thank you to whoever could provide any sort of help and examples.
in your message event you can check if the message was sent in the only-links channel if so check message.content against a RegExp() to determine whether it should be allowed or not.
if (message.channel === message.guild.channels.find(channel => channel.name === 'links-only')) {
const linkRegex = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)/g)
if (!linkRegex.test(message.content)) {
message.delete()
message.reply('this is a link-only channel').then(msg => msg.delete(5000))
}
}

Creating a Discord bot to notify people when to certain text on a website has changed

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '';
bot.on('ready', () =>{
console.log('This bot is online');
})
bot.on('message', msg=>{
if(msg.content === "?rates"){
msg.reply('.')
}
})
bot.login(token);
This is what I have so far it is very basic, I understand, I'm just trying to get some sort of idea how to process. What I want is as soon as a website gets updated or changed. I would like it to tag everyone and in a certain channel and specifies what has changed. I know this will be a long process but I'm in for the ride :) would appreciate any help.
You need a webhook on the website and listen to it with your bot, if you have control over the site, this may help you, otherwise you could look if the site has one or perhaps ask an owner.
A probably working but not very nice (and not very clean) solution would be to save the text of the website every 5 seconds or so and compare it to the previous save. If it changed, you notify the members through sending a message.

Discord.js - Get Another Channel's Topic

I'm trying to basically do what the title says.
I've tried
let channel = message.channel.get(68352726916713XXXX)
message.channel.send(channel.topic)
and stuff like that, But nothing works.
Any suggestions or answers?
If you are trying to get a channel in the same guild as the message was sent in, use message.guild.channels.get. Otherwise, use client.channels.get.
let channel = message.guild.channels.get("68352726916713XXXX");
message.channel.send(channel.topic);

how to send a message to every channel in every guild?

similar to this question however I want to be able to send it to every channel it has access to!
inside the on message event after I verify myself by ID and the issued command I was using this code:
const listedChannels = [];
msg.guild.channels.forEach(channel => {
//get all channels
client.channels.get(channel.id).send("you like bred? (message) ");
//send a message to every channel in this guild
});
however I get the error that .send is not a function...
I have been told to use .send after getting the ID of the channels
If you are looping through all of the channels, you simpily need to send your content to the channel, which you've already gotten from msg.guild.channels.forEach(channel => {//code}).
Replace what you have inside the .forEach block with;
channel.send("You like bred? (message)");
Although this will send You like bred? (message)
If you're trying to get a response back, perhaps look at this answer that explains collecting responses via reactions to a discord message.
The following explanation pertains only to v11 (stable).
Client.channels is a Collection of Channels your bot is watching. You can only send messages to text channels, and this Collection will include DM channels as well. For this reason, we can use Collection.filter() to retrieve a new Collection of only text channels within a guild. Finally, you can iterate over the channels and call TextChannel.send() on each. Because you're dealing with Promises, I'd recommend a Promise.all()/Collection.map() combination (see hyperlinked documentation).
For example...
// assuming "client" is your Discord Bot
const channels = client.channels.filter(c => c.guild && c.type === 'text');
Promise.all(channels.map(c => c.send('Hello, world!')))
.then(msgs => console.log(`${msgs.length} successfully sent.`))
.catch(console.error);
You can use client.channels for this. Check if channel type is guild text channel and then try send a message.
client.channels.forEach(channel => {
if(channel.type === 'text') channel.send('MSG').catch(console.error)
})

Resources