I am very new to javascript and I don't understand a lot of the actual code.
However, I have one query, how can I make the "cahbResponses" appear in a MessageEmbed?
I've looked at the literature on MessageEmbeds from the Discord JS guides but I still have no clue.
The code I am working on is:
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
var args = message.content.substring('!'.length).split(' ');
switch (args[0].toLowerCase()) {
case 'cahb':
var response =
cahbResponses[Math.floor(Math.random() * cahbResponses.length)];
message.channel
.send(response)
.then()
.catch(console.error);
break;
default:
break;
}
});
First, make sure you are importing Discord
// you probably have this on the beginning of your file
const Discord = require("discord.js");
Then
var response = cahbResponses [Math.floor(Math.random()*cahbResponses .length)];
// Create a MessageEmbed object and set the description
const embed = new Discord.MessageEmbed().setDescription(response);
// Then send the embed
message.channel.send(embed);
Read other MessageEmbed methods and properties here: https://discord.js.org/#/docs/main/stable/class/MessageEmbed
Related
This question already has answers here:
Discord.js bot leave guild
(5 answers)
Closed 1 year ago.
const Discord = require('discord.js')
const client = new Discord.Client({ws: {intents: Discord.Intents.ALL}});
const botowner = 'xxx'
exports.run = async (bot,message,args) => {
let guildid = message.guild.id
if (message.author.id = botowner) {
toleave = client.get_server(guildid)
await client.leave_server(toleave)
} else {message.channel.send("You are not the bot owner")}
}
exports.help = {
name: ['leavediscordserver']
}
Whenever I run this code the following shows up:
Not sure why this is happening.
The error message tells you all you need to know. You attempt to use a function called get_server(), but this function does not exist ergo you cannot use it.
A discord server in Discord.js is referred to as a Guild so to get your guild you can just call const guild = client.guilds.cache.fetch(guildid) and to leave it all you have to do is call guild.leave().
As a separate issue, a comparison in an if statement is made with 2 equal signs == not 1 as you did in your if (message.author.id = botowner) line.
var guildID = client.guilds.cache.get(args[0]) //your argument would be the server id
if (message.author.id == botowner) { // use == to check if author and bot owner id match
await guildID.leave() // waiting for bot to leave the server
} else {message.channel.send("You are not the bot owner")}
}
I am making a discord bot, and I want to do this: someone type -ping and the bot responds with #theuserwhotyped-ping.
I have no errors, I just don't know how to code it, and I don't find any solution on the Internet.
Here is the code I made:
const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = "-";
client.on("message", message=>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(" ");
const command = args.shift().toLowerCase();
if (command === "ping"){
message.channel.send("#{user}); //this is the line which dont work
}
});
You can either use message.reply:
message.reply('typed ping');
It sends a reply, like #peter, typed ping.
Or use the message.author:
message.channel.send(`${message.author} typed ping`);
It sends a message like #peter typed ping.
I have been trying to make a Discord bot that responds to the user when they ask for help. Sadly I can't seem to get the bot to work. I am using Discord.JS on Node.JS. Any help would be welcome.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '[token removed]';
const PREFIX = '!';
bot.on('ready', () => {
console.log('K08 Help Bot is online!');
})
bot.on('message', message=>{
let args = message.content.substring(PREFIX.lenght).split(" ");
switch(args[0]){
case 'help':
message.reply('HELLO');
break;
}
})
bot.login(token);
This might be the issue:
let args = message.content.substring(PREFIX.lenght).split(" "); - length is mistyped as lenght
On a side note: I've submitted an edit to hide your token in this question. For security, you should never share your token; it would allow someone to take over your bot!
When im coding the part of the discord bot that gives a role when activated i keep getting this error and im not sure if there is a bug i dont see but if you see it pls help me correct it!
Error message.
if(mesage.content.startsWith(prefix + "prune")){
^
ReferenceError: mesage is not defined
Section of script with problem.
if(mesage.content.startsWith(prefix + "prune")){
let args = Message.content.split(" ").slice(1);
let author = Message.member;
let role = message.guilds.roles.find('name', "Moderator");
if(author.roles.has(role.id)){
if(!args[0]){
Message.delete();
Message.author.send("No arguments given.");
return;
}
}
}
Full Script
const Discord = require('discord.js')
const Client = new Discord.Client
const prefix = "/";
Client.on('ready', ()=>{
console.log('Bot is online.');
})
Client.on('message', (Message)=>{
if(!Message.content.startsWith(prefix)) return;
if(Message.content.startsWith(prefix + "hello")){
Message.channel.send("Hello.");
}
if(Message.content.startsWith(prefix + "help")){
Message.channel.send("The only avaible command right now is /help and /hello.")
Message.author.send("This is only for test purposes!");
}
if(mesage.content.startsWith(prefix + "prune")){
let args = Message.content.split(" ").slice(1);
let author = Message.member;
let role = message.guilds.roles.find('name', "Moderator");
if(author.roles.has(role.id)){
if(!args[0]){
Message.delete();
Message.author.send("No arguments given.");
return;
}
}
}
})
Client.login("<Bot Token>");
All your code refers to Message, except this line which is mesage, not only mis-spelled, but incorrectly lower-case.
Making that consistent with the other things should fix the issue.
Note that JavaScript generally reserves capital letters for things like classes, lower-case for variables and arguments. As you can see here the syntax highlighter thinks this is a class and is colouring it accordingly. Lower-case message is the conventional argument name.
Change mesage (the typo) with message (not uppercase). Make sure to also change Message with message (again, no uppercase)
The Issue
When executing my code i am really getting no errors at the moment but would like to add to a function but have tried almost every way of handling it. At this point the variable has been removed due to confusion and frustration.
What needs to happen is, the User that initiates the command, their message gets deleted after a short delay. I have tried message.delete(1000) and other Variants for v12 but no luck. Not able to pass the "message" variable from my active function?
Maybe i am completely off and just making it hard on myself, i don't know. Honestly embarrassing that i couldn't figure out a message.delete. Please help. Apologies for the ignorance.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = "";
const PREFIX = "!";
const fs = require('fs');
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
bot.on('ready', () => {
console.log("The bot is active and ready to go!");
});
bot.on('message', function(message) {
if(message.content[0] === PREFIX) {
let command = message.content.substring(message.content.indexOf(" ") + 1, message.content.length);
}
});
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case "crash":
bot.commands.get('crash').execute(message, args);
break;
case "hello":
bot.commands.get('hello').execute(message, args);
break;
case "purge":
bot.commands.get('purge').execute(message, args);
break;
}
});
bot.login(token);
Here is an example of "crash.js" for reference. Don't know if i need to execute delete from there?
module.exports = {
name: 'crash',
description: "Crash",
execute(message, args){
message.author.send("Here is the link you requested");
}
}
You can execute delete from within your module. The message is passed as a full object so you just call the delete method on it. However, the Options are an Object which means it needs to be defined as such. For clarity, I'm going to use another variable but this can be done inline.
let options = {
timeout: 1000,
reason: 'Because I said so.'
}
message.delete(options);
or inline...
message.delete({timeout: 1000});