Discord Bot Server Deafen - javascript

I'm trying to mess with my friend so whenever he tries to user another discord bot command starting with "-", it deafens him so he can't hear it. I've been searching everywhere trying to find a solution but I wasn't able to find one that worked. I tried using the guild.fetchMember() function, but I'm not sure if I did it correctly so it didn't work. This is what I have so far (only the last function is in question):
My code

Friend, You can do this by
bot.on("message", message =>{
// confirming that its your friend who is using the command.
if(message.author.id != "your friend's id") return;
if(message.content.startsWith("-")){
message.guild.members.cache.find(member => member.id == "your friend's id").voice.serverDeaf;
};
});

Related

Discord.JS Message in a specific Channel

So i'm new to the whole Discord.JS thing and i am trying to figure out how to get the bot to write a message to the General Chat when a new person joins.
I have seen a lot of people doing it like this:
const channel = client.channels.cache.find(channel => channel.name === channelName)
channel.send(message)
But this isnt working for me. Whenever i am trying to send a message with the channel.send(message) it gives me a error message.
I have also tried the version where you do it with the client.channels.cache.get(<Channel-ID>)
But this also didnt work for me.
Use this code similar to the post: how to send a message to specific channel Discord.js
<client>.channels.fetch('<id>').then(channel => channel.send('<content>'))
Example
client.channels.fetch('922880975074127872')
.then(channel => channel.send(`Welcome, ${user}`))

Discord bot reacts when specific user is tagged

I'm new to programming and i'm trying to make a Discord bot for me and my friends, for now I have set up two commands, the first works fine, but the second one is the problem.
client.on('message', msg => {
if (msg.content === `#usertag#0001`) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me`);
}
});
So what it should do is when a specific user is tagged it sends a message, the message sent from the bot is working (it tags the user who sent the message 3 times and then it says something else), but the part where the bot recognizes the tag doesn't work.
I tried by putting the Discord id, using the full #, and other stuff, but it doesn't seem to work.
For this you could use the message.mentions method, and here is how you could use it for example:
// For Discord V13 'message' is deprecated and
// will be removed in the future, use 'messageCreate' instead.
client.on("message", msg => {
// Disable messages from bot to avoid endless loops / replies
if(msg.author.bot === true) return;
// Check if anyone is mentioned.
// Skip #everyone mentions to be checked.
if(msg.mentions.users.size > 0 && msg.mentions.everyone === false) {
if(msg.mentions.users.find(user => user.id === "ENTER USER ID HERE")) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me!`);
}
}
});
Hope this will help you! ! I have tested this with Discord V13, i'm not 100% sure if this will work on Discord V12 as well.

make an autorole for discord.js v12

I wanna make my bot give automatically role when my friends join for first time to my server: I have tried this one my VPS node version: 12.19.0v:?
client.on('guildMemberAdd', member => {
console.log('User #' + member.user.tag + ' has joined the server!');
var role = member.guild.roles.cache.find(role => role.name == "Newbie")
let user = member.user
user.roles.add(role);
});
but nothing is working ! help me pls
Discord is now enforcing privileged intents. The GUILD_MEMBERS intent is required to receive events such as guildMemberAdd and guildMemberUpdate.
To learn how intents work and how to use them, check out discord.js' detailed guide.
Also, as stated in the comments, you need to use message.member instead of message.user
You need to know the difference between Members and Users. A user represents a Discord account, a member represents a user being in a server. So for example to get someone's tag, you need to do user.tag, member.tag wouldn't work. It's the same with nicknames, user.displayName woudln't work, because a user is an account, a member is a user in a server. You'd do member.displayName instead.
Anyway, where you put
let user = member.user
user.roles.add(role);
it should be replaced with
member.roles.add(role);
and that should hopefully solve your problem.

discord.js sending message to specific channel

I've been looking around, can't quite seem to find the answer to this issue I am having with a discord bot I am making with Typescript. I have all of my commands in their own folder using a separate file for each command. Helps to keep things organised.
I've seen people say client.channels.get(`channelID`).send(`Text`)
but that's giving me
Object is possibly 'undefined'. and Property 'send' does not exist on type 'Channel'.
I'm actually trying to make a bot message every text channel (given from a list) whenever someone runs a reboot command because for whatever reason people keep rebooting the bot. I implemented it as a funny thing to do every now and again as a troll if someone needs to use it. The bot goes offline for 3 minutes but I don't like having people spam it and pretty much have the bot un-usable.
I'm using client.channels.get(channels.channelnames[5]).send("This is a message.")
Solution:
if(msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel) {
var txtchannel = msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel
txtchannel.send("This is a message in a channel. Don't know why you read this.")
}
so I was on the right track mostly. just had to do as Discord.TextChannel and I think that's why Cynthia was saying about casting the variable as a TextChannel
This code works. thanks for all your help guys!
According to https://discord.js.org/#/docs/main/stable/class/Collection it seems like there is no get method.
Try client.channels[channels.channelnames[5]].send("This is a message.")
In other words try to replace .get with the square brackets.
EDIT: Sorry I was a bit quick, I think the issue is type casting, try casting the Channel to a TextChannel if you know that it is a text channel.
This should work assuming your channels are text ones.
client.on('ready',(e)=>{
let channel_ids = ['123','456','789'];
// loop through the list of channel ids.
for(let i=0, l=channel_ids.length; i<l; i++){
let channel_id = channel_ids[i];
let this_channel = client.channels.get( channel_id );
// if exists, and type in list send message
if(this_channel && ['dm', 'group', 'text'].indexOf( this_channel.type ) != -1){
this_channel.send('a cool message')
.then(message => console.log(`Sent message: ${message.content}`))
.catch(console.error);
}
}
});

discord.js bot replies to itself

I am currently coding my first discord bot, it can already play YouTube music.
if (message.content.includes("Good Job") ||
message.content.includes("good job")) {
message.channel.sendMessage("Good Job everyone :smirk:");
}
As you see, if someone types "good job" (this is just an example) then the bot will reply with "good job everyone :smirk:), but then the spam will begin: the bot reads his own message and replies to it.
How can I prevent the bot from answering itself?
Use this in the on message event:
if (message.author.bot) return;
for more info:
https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/a-basic-command-handler.html
The reason why your bot replies to yourself is because where you put:
if (message.content.includes("Good Job") ||
message.content.includes("good job"))
It is basically checking the chat if a piece of text includes the words "Good Job" or "good job". As your bot sends:
message.channel.sendMessage("Good Job everyone :smirk:");
as an answer, it creates a loop because that message includes the words "Good Job", basically running the code again and again and again.
Of course, the easiest way of fixing this is for you to change the answer it gives to not include the words Good Job, but there is better solution to make sure it doesn't happen for all of the commands you might make.
As #Jörmungandr said, under the message event include:
if (message.author.bot) return;
to make sure it doesn't happen. It essentially checks if the author of the message is a bot and if it is, it ignores it.
discord.js
// In message event
if(message.author.id === client.user.id) return;
// I would recommend a variable like this for splits on words
// const args = message.content.trim().split(/\s+/g);
// You could also .slice() off a prefix if you have one
if(/good job/i.test(message.content)) {
message.channel.send('Good job everyone :smirk:'); // sendMessage is deprecated, use send instead
}
You can simply just check if the user sending the message is a bot. For example:
if (!msg.author.bot) {
<Your code to execute if the user is not a bot.>
}
Hope this was helpful, thank you!
You may use this code which avoids doing anything if the author is a bot:
if(message.author.bot) return;
There are quite a lot of ways to solve this.
1: Stopping the Bot from Responding to ANY bot.
Right under your message callback (the client.once('message')), you can just add:
if (message.author.bot) return;
2: Stopping the Bot from Responding to itself
Once again, right under your message callback, you just add:
if (message.author.id == client.user.id) return;
Please note the client that I am referring to is your bot's client, created when you type
const client = new Discord.Client()
...or whatever your client is. It's basically the thing that you login to (ex: client.login(yourtokenhere))
Most tutorials will call this Client, client, or maybe even bot. The discord.js guide will call it a Client.

Categories

Resources