Eval command doesn't work at all, but it doesn't error - discord

I'm trying to make an eval command for my bot. It doesn't error, but it doesn't send a message to the console or the discord channel. Heres my eval code:
const clean = async (client, text) => {
if (text && text.constructor.name == "Promise")
text = await text;
if (typeof text !== "string")
text = require("util").inspect(text, { depth: 1 });
text = text
.replace(/`/g, "`" + String.fromCharCode(8203))
.replace(/#/g, "#" + String.fromCharCode(8203));
text = text.replaceAll(client.token, "[REDACTED]");
return text;
}
client.on("messageCreate", async (message) => {
const args = message.content.split(" ").slice(1);
if (message.content.startsWith(`${p}eval`)) {
if (message.author.id !== 821682594830614578) {
return;
}
try {
const evaled = eval(args.join(" "));
const cleaned = await clean(client, evaled);
message.channel.send(`\`\`\`js\n${cleaned}\n\`\`\``);
} catch (err) {
message.channel.send(`\`ERROR\` \`\`\`xl\n${cleaned}\n\`\`\``);
}
}
});
Let me know if I have to give you more code.

It seems like you put a number as your ID... Discord.js IDs are in strings so you should put your ID into a string.
if (message.author.id !== "821682594830614578") {
return;
}

Probably your Discord ID is wrong. Tell me your discord username, I will add you as friend and will solve it in DMs.
This is my Discord Username Nishant1500#9735

Related

How to make a say embed command in Discord.js

I'm new to coding and am making a Discord bot for a friend. They are requesting a say command that could act as a confession command where it would look like this. An embed with a set Title, set color, and completely anonymous but with an editable description that would fill in with what they want to confess. As i'm new to coding I don't know how to do this. If anyone can help that would be really appreciated! Thank you!
(Edit) I realise that i wasn't through enough about what the code was so im making an edit with my main.js code.
const client = new Discord.Client();
const prefix = 'wtf ';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
bot.once('ready', () => {
console.log('Tomoko is online!');
});
bot.on('message', async msg =>{
if(!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(msg, args, Discord);
} else if (command === 'creator'){
client.commands.get('creator').execute(msg, args, Discord);
} else if (command === 'help'){
client.commands.get('help').execute(msg, args, Discord);
} else if (command === 'kick'){
client.commands.get('kick').execute(msg, args, Discord);
} else if (command === 'ban'){
client.commands.get('ban').execute(msg, args, Discord);
} else if (command === 'mute'){
client.commands.get('mute').execute(msg, args, Discord);
} else if (command === 'unmute'){
client.commands.get('unmute').execute(msg, args, Discord);
} else if (command === 'warn'){
client.commands.get('warn').execute(msg, args)
} else if (command === 'deletewarns'){
client.commands.get('deletewarns').execute(msg, args);
} else if (command === 'warnings'){
client.commands.get('warnings').execute(msg, args);
} if (args[0].toLowerCase() === 'confess') {
const description = args.splice(1).join(" ");
const embed = new MessageEmbed().setTitle('✦┊‧๑ ꒰<a:ccc:862524564457390150><a:ooo:862524674185101322><a:nnn:862524667368833024><a:fff:862524592202973244><a:eee:862524583802568714><a:sss:862524709782683648><a:sss:862524709782683648>꒱ ‧๑┊✧').setColor('ffaaaa').setDescription(description);
await msg.delete().catch(e => console.log(e));
msg.channel.send(embed);
} else if (command === "unban"){
client.commands.get('unban').execute(msg, args, Discord);
;}
});
client.login('DAMN YOU WISH I WOULD SHOW YOU');
So if possible can anyone give me the advanced command handler say embed command. Thank you!!
That depends on how you handle your commands. But in general: generate a new embed, and set the description to the contents of your message.
Check here on how to create an embed.
A simple bot to do your job
// importing dependencies
const { MessageEmbed, Client } = require('discord.js');
const prefix = '!';
const bot = new Client(); // init discord client
bot.on('ready', () => console.log('yee im on'));
// listening for messages
bot.on('message', async msg => {
if (!msg.content.startsWith(prefix)) return // dont run if the prefix is not used
const args = msg.content.substring(prefix.length).split(" "); // creating array of the message contents
if (args[0].toLowerCase() === 'say') { // a simple command handler
const description = args.splice(1).join(" ");
const embed = new MessageEmbed().setDescription(description); // setTitle and stuff according to your preference
await msg.delete().catch(e => console.log(e)); // deleting the user message since it should be anonymous
msg.channel.send(embed);
}
});
bot.login('yourtokenhere');
Make sure to replace token and prefix with your token and prefix
How to run the command :
!say ooh this is a confession

discord.js Welcome message

yea hello I'm making a discord.js bot and I have this code currently and it REFUSES to send a message (it doesn't error either
let chx = db.get(`welchannel_${member.guild.id}`);
if (chx === null) {
return;
}
let data = await canva.welcome(member, { link: "https://i.pinimg.com/originals/f3/1c/39/f31c39d56512dc8fbf30f9d0fb3ee9d3.jpg" })
const attachment = new discord.MessageAttachment(
data,
"welcome-image.png"
);
client.channels.cache.get(chx).send("Welcome to our Server " + member.user.username, attachment);
});
and then i have welcome.js with this code but it aint sending and i cant figure out why ...
const db = require("quick.db")
module.exports = {
name: "setwelcome",
category: "moderation",
usage: "setwelcome <#channel>",
description: "Set the welcome channel",
run: (client, message, args) => {
let channel = message.mentions.channels.first()
if(!channel) {
return message.channel.send("Please Mention the channel first")
}
//Now we gonna use quick.db
db.set(`welchannel_${message.guild.id}`, channel.id)
message.channel.send(`Welcome Channel is set to ${channel}`)
}
}```
just a guess that your not checking for undefined on CHX
if (chx === null && chx === undefined) {
return;
}
This is my modified code from my discord bot (just to keep it general).
this.client.on('ready', () => {
if(botChannelID){
this.client.channels.fetch(botChannelID).then(ch => {
ch.send(`Welcome message here.`);
}).catch(e => {
console.error(`Error: ${e}`);
});
}
//Other stuff. Blah blah blah...
});

Banned user cant be found in BansCollection using fetchBans()

ok so i made sure to check ids,
the user im trying to unban is banned.
the id is correct
i tried to console log what userBanned returns and it is undefined
like how?
const text = args.join(` `)
const bansCollection = await message.guild.fetchBans()
const userBanned = bansCollection.find(user => user.id == text)
if (!isNaN(text)) {
if (userBanned) {
await message.guild.members.unban(text)
message.channel.send(`<#${text}> has been unbanned`)
} else { message.channel.send(new Discord.MessageEmbed().setTitle(`**That user is not Banned!**`)) }
} else { message.channel.send(`You Need the user's ID!`) }
console.log(text , bansCollection, userBanned)}
I would fetch the audit logs nd filter them.
Example:
message.guild.fetchAuditLogs()
.then(logs => {
let ban = logs.entries.filter(e => e.action === 'MEMBER_BAN_ADD') //get all the bans
ban = ban.filter(e => e.target.id === text) //filter the bans from the user
if (!message.guild.members.cache.get(text)) return message.channel.send(`This user is not in this server.`);
if (userBanned) {
await message.guild.members.unban(text)
message.channel.send(`<#${text}> has been unbanned`);
} else {
let embed = new Discord.MessageEmbed();
embed.setTitle(`**That user is not Banned!**`);
message.channel.send(embed);
}
});

How to add a prefix discord.js

require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
const prefix = ".";
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
bot.on('message', msg => {
if (msg.content === 'ping') {
msg.channel.send('pong');
} else if (msg.content.startsWith('!kick')) {
if (msg.mentions.users.size) {
const taggedUser = msg.mentions.users.first();
msg.channel.send(`You wanted to kick: ${taggedUser.username}`);
} else {
msg.reply('Please tag a valid user!');
}
}
});
currently that is what im using im trying to make it so i have to type .pingto get the message Pong but i cant figure out how to get the prefix to use
You could use this code to get the command name:
bot.on('message', msg => {
const command = msg.content.slice(prefix.length).split(' ')[0]
if (command === 'ping') {
msg.channel.send('pong');
} else if (command === 'kick') {
if (msg.mentions.users.size) {
const taggedUser = msg.mentions.users.first();
msg.channel.send(`You wanted to kick: ${taggedUser.username}`);
} else {
msg.reply('Please tag a valid user!');
}
}
});
Note that this changes the kick command from !kick to .kick.
I recommend reading this section of the Discord.js guide for how to setup commands (and also user arguments if you need).
You can check for a concatenation of the prefix and the command name.
To check for .ping where prefix is .
if (msg.content === prefix + 'ping') {
// do something
}
To make it safer you can make it return early by checking if the message starts with the prefix.
if (!msg.content.startWith(prefix)) {
return;
}
This way when that check passes, you can slice away the prefix, and check the command name directly without having to worry about the prefix.
const commandName = msg.content.slice(prefix.length); // remove prefix character(s)
if (commandName === 'ping') {
// do something
}
All together it'd look like:
const prefix = '.'
bot.on('message', msg => {
// not start with prefix -> return early
if (!msg.content.startWith(prefix)) return;
// remove prefix to get command name
const commandName = msg.content.slice(prefix.length);
// check for command name directly
if (commandName === 'ping') {
// do something
} else if (commandName === 'kick') {
// do another thing
}
}

Random Meme Command (discord.js v12)

I'm trying to make a 'random meme' command for my Discord Bot. I'm new to working with APIs, but I've tried my best.
The problem is, when I type the command, nothing happens. There are no errors, but the bot doesn't send anything in discord.
This is my code:
if (command === "meme")
async (client, message, args) => {
const subReddits = ["dankmeme", "meme", "me_irl"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const img = await randomPuppy(random);
const embed = new Discord.MessageEmbed()
.setColor(16776960)
.setFooter("test")
.setImage(img)
.setTitle(`Random Meme requested by <#${message.author.tag}>`)
.setURL(`https://reddit.com/r/${random}`)
message.channel.send(embed);
}
Here Is One That Will Show Info About The Meme
if(command === "meme") {
const subReddits = ["dankmeme", "meme", "me_irl"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
try {
const { body } = await snekfetch
.get('https://www.reddit.com/r/${random}.json?sort=top&t=week')
.query({ limit: 800 });
const allowed = message.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18);
if (!allowed.length) return message.channel.send('It seems we are out of memes');
const randomnumber = Math.floor(Math.random() * allowed.length)
const embed = new Discord.RichEmbed()
.setColor(0x00A2E8)
.setTitle(allowed[randomnumber].data.title)
.setDescription("Posted by: " + allowed[randomnumber].data.author)
.setImage(allowed[randomnumber].data.url)
.addField("Other info:", "Up votes: " + allowed[randomnumber].data.ups + " / Comments: " + allowed[randomnumber].data.num_comments)
.setFooter("r/" + random)
message.channel.send(embed)
} catch (err) {
return console.log(err);
}
}
Let Me Know If It Don't Work, But I Should
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === "meme") {
async (client, message, args) =>
const subReddits = ["dankmeme", "meme", "me_irl"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const img = await randomPuppy(random);
const embed = new Discord.MessageEmbed()
.setColor(16776960)
.setFooter("test")
.setImage(img)
.setTitle(`Random Meme requested by <#${message.author.tag}>`)
.setURL(`https://reddit.com/r/${random}`)
message.channel.send(embed);
}
});
This should work, not quite sure, haven't tested it. (You can put in a command handler your self)
if (command === "meme")
async (client, message, args) => {
const fetch = require('node-fetch');
let userAvatar = message.author.avatarURL({ format: "png", dynamic: true, size: 2048 }); // this is just the users icon, u can remove it if you want.
fetch(`https://meme-api.herokuapp.com/gimme`)
.then(res => res.json())
.then(async json => {
const embed = new MessageEmbed()
.setAuthor(`${json.title}`, `${userAvatar + "?size=2048"}`, `${json.postLink}`)
.setImage(`${json.url}`)
.setFooter(`👍${json.ups} | ${json.subreddit}`)
.setColor("RANDOM")
message.channel.send(embed).catch((error) => {
console.log("An error has occured on the \"meme\" command\n", error)
})
}
Here you go! I've tested this on my own command handler.

Resources