making a discord bot say the rules when aksed - javascript

hello I am trying to code a Discord bot, I am trying to make it so that if someone says $rules the rules will be sent, I can't send the code because I have a problem with posting something on this website, I have searched a lot on the internet but I can not find anything
edit: I have found out how I can post the code https://glitch.com/edit/#!/melted-messy-leotard
this is the link to see it

You have a syntax error in your code - you never close the listener with any brackets.
To fix this you can use this:
let Discord = require("discord.js");
let client = new Discord.Client();
client.on("message", message => {
if (message.content === "ping") {
message.channel.send("pong!")
}
if(message.content === "embed") {
let embed = new Discord.MessageEmbed()
.setTitle("this is Emded title")
.setDescription("this is Embed description")
.setColor("RANDOM")
.setFooter("This is embed footer")
message.channel.send(embed)
}
if (message.content.startswith("$rules")) {
let member = message.mentions.members.first()
if (!member) message.send("here are the rules, 1. no politics 2. be nice 3. respect others 4. think before you speak 5. no nsfw 6. don't leak personal info 7.don't argue abt opinions, 3 strikes and you're out")
}
});
client.login("token");
Also your line where you do if(!member) probably won't do what you want it to do - this will respond if it DMs the bot, if this is what you wanted then it is fine.
If you want to check if the member is in a guild, you can check if they have a common role (such as everyone) using this:
if (!member.roles.cache.some(role => role.name === '<role name>')) {
// member is not in guild
}

Related

Name shows up as "undefined"

So, I have been working on a kick command, and I pretty much got the whole thing working, except when I have the bot send a message telling us who was kicked, it just shows up as "undefined was kicked"
client.on("message", message => {
if (message.content.startsWith('grimm!kick')) {
var member = message.mentions.members.first();
if (message.member.hasPermission('KICK_MEMBERS')) {
const kicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.mentions.members.first.username} Was kicked`)
message.channel.send(kicked)
member.kick()
} else {
const notKicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.author.username}! you do not have permission to kick people! you must have the "Kick Members" Permission to use this command`)
message.channel.send(notKicked)
}}});
I have tried "member.username" and that still wont work
Could someone tell me what exactly I'm doing wrong, so I wont make this same mistake again? Thanks!
There are 2 problems in this code.
You are using first as a property, not a method.
the property username doesnt exist on The GuildMember class.
what you are looking for is GuildMember.user.username.
replace
.setDescription(`${message.mentions.members.first.username} Was kicked`)
with
.setDescription(`${message.mentions.members.first().user.username} Was kicked`)

discord.js mention command not picking up users but picks up itself

I've started my bot after a while and for some reason the mention command is not working. Code and response to the command is below:
client.on('message', message => {
if (message.content.startsWith('L!bite')) {
let targetMember = message.mentions.members.first();
if (!targetMember) return message.reply('you need to tag a user in order to bite them!!');
// message goes below!
message.channel.send(`${targetMember.user}, You just got a bitten!`);
message.channel.send({files:["./nom/"]})
//let embed = new Discord.RichEmbed()
//embed.setImage(`https://media1.tenor.com/images/4f5666861516073c1c8015b2af7e2d15/tenor.gif?itemid=14720869`)
message.channel.send(embed);
}
});
Response on Discord: (it does picks itself up as a user but not anyone else)
Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·Today at 1:18 PM
L!bite #Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·
._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.BOT Today at 1:18 PM
#Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·, you need to tag a user in order to bite them!!
I'm just confused on why it's saying that I'm or other people are not users, but it picks up itself? E.g.:
Β·._.β€’πŸŽ€ πΉπ΅πΌπΏπŸ’—πΏπΌπ’’πΌπ‘…πΏ πŸŽ€β€’._.Β·Today at 1:18 PM
L!bite #._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.
._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._.BOT Today at 1:18 PM
#._.Β·πŸŽ€πΉπ΅πΌπΏβ™‘π“π’Ύπ”Ÿπ“Έπ“£πŸŽ€Β·._., You just got a bitten!
GIF HERE
Try converting the message.mentions.members to array and then filter your bot! -
const mentions = message.mentions.members.array() // will return members array of GuildMember class
const firstMention = mentions.filter(mention=> !mention.user.id === "BOT_ID")[0]
Alternatively, there's already a parameter you can implement in your code where it can automatically check if the message came from a bot. Although, this process may not work if you want to have other bots automatically tag it, but I think that's unlikely.
if (message.author.bot) return; FYI, not sure of which version of discord.js you're using, but the embed naming convention has changed. Instead of RichEmbed(), you should use MessageEmbed().
So the place you would implement this is right after your first if conditional block, like this:
if (message.content.startsWith('L!bite')) {
//the thing that checks if the user who posted was the bot
if (message.author.bot) return;
Hopefully this helps!

discord.js embed profile picture command with mentions

I'm making a bot in discord.js using the Visual Studio Code app. I'm trying to create a command for profile pictures so when you type -pfp it would show you your profile picture and when you type -pfp #user it would show the person's you mentioned profile picture. (- is the prefix). Though the bot only sends the message without the embed part with the picture. When I mention someone else it does the same thing but mentioning me and not the user.
This is what I have:
if (!message.content.startsWith(prefix) || message.author.bot) return;
if (message.content.startsWith(prefix + 'pfp')) {
message.channel.send('Here is <#'+ message.author.id+ ">'s pfp :)")
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor('user.username')
.setImage(message.author.displayAvatarURL());
} else if (message.content.startsWith(prefix+ 'pfp'+ message.mentions.users)) {
message.channel.send('Here is <#'+ message.user.id+ ">'s pfp :)")
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor('user.username')
.setImage(message.user.displayAvatarURL());
}
});
There are two parts here.
First, the bot is only sending the message and not the embed because you only ever send the message. You need a separate line of code to send the embeds.
message.channel.send(avatarEmbed);
Secondly, the bot only ever tags you because of this message.content.startsWith(prefix + 'pfp'). The way you devide between the author and someone else means that it will always match the first case, meaning that the message always starts with prefix + pfp regardless if you tag someone after that.
Now you have a few ways to fix this but I would do it this way.
First you define a new variable, lets call that pfpMember, and you assign that to either the first person you tag or the author of the message.
var pfpMember = message.mentions.members.first() || message.member;
Now that we have a fixed member that is either someone who gets tagged or the author we can just assign the displayAvatarURL function to that member.
.setImage(pfpMember.user.displayAvatarURL());
So your entire command should look a little something like this.
if (message.content.startsWith(prefix + 'pfp')) {
var pfpMember = message.mentions.members.first() || message.member;
// we can just put the member object into the string here, that will tag the person
message.channel.send(`Here is ${pfpMember}'s pfp :)`);
const avatarEmbed = new Discord.MessageEmbed()
.setColor('#446580')
.setAuthor(pfpMemer.user.username)
.setImage(pfpMember.user.displayAvatarURL());
message.channel.send(avatarEmbed);
}

Temporary Ban Command Issues

I was trying to make a temporary ban command for a discord bot that I am currently developing, and I keep running into an issue (node:15340) UnhandledPromiseRejectionWarning: ReferenceError: ban is not defined and I was trying to see if there is any guide or way to step so it can work, what I was trying to do is make when a command was executed it temporary bans someone and it displays the reason why in discord's audit log, also to send a embed saying who was the user banned by for what, so when someone would execute the command it would look like this - !ban (user) (time) (reason).
let toBan = message.mentions.members.first();
let reason = args.slice(2).join(" ");
let member = message.guild.member(toBan)
if(!args[0]) return message.channel.send('Please mention some to ban, so I can ban that fooly!');
if(!toBan) return message.channel.send(`${args[0]} is not a member of this guild, fooly.`);
if(!reason) return message.channel.send('Please specify a reason for the ban fooly!');
if(!toBan.bannable){
return message.channel.send("This dude has super powers, fooly. I can't ban this person.")
}
if(member) {
toBan.ban({
reason: `${reason}`
})
}
let bantime = args[1];
if(!bantime) return message.reply("Fooly! You need to provide a time!");
await(bantime(ban));
message.reply(`Fooly has been banned for ${ms(ms(bantime))}`);
if(toBan.bannable){
const messageEmbed = new Discord.MessageEmbed()
.setTitle('Fooly was banned')
.addField('Person that was banned:', toBan)
.addField('Banned by:', message.author)
.addField('Reason:', reason)
.setTimestamp(Date.now())
.setColor('#FF0000')
.setFooter('Fooly Discord Bot');
message.channel.send(messageEmbed);
toBan.ban();
}
setTimeout(function(){
message.guild.members.unban(toBan)
message.channel.send(`<#${toBan.id}> has been unbanned (temporary ban is finished).`);
}, ms(bantime));
}
}
So firstly, bantime is a string from the message and not a function, so that would return an error later on.
Secondly, await(bantime(ban)); is giving you the error you specified, and it is exactly as it says. You do not have ban declared anywhere, thus it is unable to know what ban is referring to. You might be looking for
let bantime = args[1];
if(!bantime) return message.reply("Fooly! You need to provide a time!");
message.reply(`Fooly has been banned for ${ms(ms(bantime))}`);
I just removed the await(bantime(ban)) in the snippet above, I don't think there's a purpose for it after looking at your code.

Can't send embed message on join

No idea why it won't work, the exact code worked on my old bot. Code:
client.on("guildMemberAdd", member => {
const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
.setTitle("**Please be sure to read our rules carefully thanks**")
.setAuthor("Welcome to BACKUP")
.setColor(3447003)
.setDescription("Please enjoy your stay")
.setThumbnail(message.author.avatarURL)
client.channels.get('505107608391254030').send({embed});
})
}
The thing that confuses me most, is that if I replace that code with this code, it works fine.
client.on('guildMemberAdd', member => {
member.guild.channels.get('505107608391254030').send("This works, but embed does not, fix it boi, line 102");
});
(On the code that did not work, I tried: client.channels.get, member.channels.get, member.guild.channels.get, client.guild.channels.get
The problem is when you are finding the channel, client.channels.get isn't a method. doesn't work in this situation for reasons I'm not aware of
You have to use client.guilds.get(GUILD_ID).channels.get(CHANNEL_ID).send({embed});

Categories

Resources