I'm trying to make logs for my server, so when someone sends, deletes or edits a message, the bot will send an embed message in a specific channel with all the information. For some reason, the messageUpdate and messagedDelete events are triggered multiple times, and I can't understand why. I have tried everything, but nothing works. Why does this happen and how can I fix it?
I have the logs for each thing (sent, edited, deleted) in individual files, by the way. In my main file, this is the code I have for the messageUpdate:
bot.on('messageUpdate', (oldMessage, newMessage) => {
if (oldMessage.author.id === bot.user.id) return;
if(oldMessage.content === newMessage.content) return;
bot.events.get('editlogs').execute(oldMessage, newMessage);
})
This is my code for the edit logs:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login(process.env.TOKEN);
module.exports = {
name: 'editlogs',
description: 'logs messages that were edited.',
category: 'logs',
execute(oldMessage, newMessage){
const logsChannel = bot.channels.cache.get("743142195594526822")
const logEmbed = new Discord.MessageEmbed()
.setColor('#ffa600')
.setTitle('Message Edited')
.setAuthor(oldMessage.author.tag, oldMessage.author.avatarURL())
.setThumbnail(oldMessage.author.avatarURL())
.addFields(
{ name: 'Old Message', value: oldMessage.content},
{ name: 'New Message', value: newMessage.content},
{ name: 'Channel', value: oldMessage.channel.toString(), inline: true },
{ name: 'Author', value: oldMessage.author, inline: true },
{ name: 'Author ID', value: oldMessage.author.id},
{ name: 'Message ID', value: oldMessage.id},
{ name: 'Message', value: '[Jump To Message](' + oldMessage.url + ')'},
)
.setTimestamp()
.setFooter('Edited in ' + oldMessage.guild.toString(), oldMessage.guild.iconURL());
logsChannel.send(logEmbed);
}}
Related
first time posting here. I hope I've posted this correctly, but basically getting the error message "interaction.options.getMember is not a function" I've tried using getUser as well (someone suggested this) and it doesnt seem to fix the problem. I'm pretty new to all this so please go easy on me. as far as I can see everything has been defined correctly and I'm pretty sure the issue is from here somewhere as the rest of the commands work absolutely fine. Any help greatly appreciated!!
const { Client, CommandInteraction, MessageEmbed } = require("discord.js");
module.exports = {
name: "ban",
description: "Bans the target member",
permission: "ADMINISTRATOR",
options: [
{
name: 'target',
description: "Select a target to ban",
type: "USER",
required: true,
},
{
name: 'reason',
description: "Provide a reason for the ban",
type: "STRING",
required: true,
},
{
name: 'messages',
description: "Choose one of the choices",
type: "STRING",
required: true,
choices: [
{
name: "Don't delete any",
value: "0"
},
{
name: "Previous 7 days",
value: "7"
}
]
},
],
/**
*
* #param {Client} client
* #param {CommandInteraction} interaction
*/
execute(client, interaction) {
//ERROR IS LINE BELOW//
const Target = interaction.options.getMember('target');
if (Target.id === interaction.member.id)
return interaction.followUp({embeds: [new MessageEmbed().setColor("RED").setDescription(`⛔ You can't ban yourself.`)]})
if (Target.permissions.has("ADMINISTRATOR"))
return interaction.followUp({embeds: [new MessageEmbed().setColor("RED").setDescription(`⛔ You can't ban an administrator.`)]})
const Reason = interaction.options.getString('reason');
if (Reason.length > 512)
return interaction.followUp({embeds: [new MessageEmbed().setColor("RED").setDescription(`⛔ The reason cannot exceed 512 characters`)]})
const Amount = interaction.options.getString('messages')
Target.ban({days: Amount, reason: Reason})
interaction.followUp({embeds: [new MessageEmbed().setColor("GREEN").setDescription(`✅ **${Target.user.username}** has been banned!`)]})
}
}
According to your code, and per Discord API, you have your member (actually user) here:
name: 'target',
description: "Select a target to ban",
type: "USER",
required: true,
so that line should read
const Target = interaction.options.getUser('target');
You need to get the member by using the below code
const targetMember = interaction.guild.members.cache.find(member => member.id === Target)
The rest needs to change Target to targetMember or simplify with example below:
if (targetMember.permissions.has(Permissions.FLAGS.BAN_MEMBERS) || targetMember.user.bot || targetMember.user.id === interaction.user.id) {
return interaction.reply({
content: 'You are not authorized to ban this member or yourself.',
ephemeral: true
})
I actually managed to fix the issue before I went sleep at 4am. Essentially the problem was actually caused by the execute (client, interaction). In my other file, these parameters are (interaction, client). I also changed all instances of "followUp" with "reply" and it started working as intended.
Thanks for helping out though!
So simple emoji command where it's supposed to display it's information like name, id, link and etc you get the idea. Works all fine with default emojis and custom server emojis but the moment its animated the name changes from
"<a:CS_Shrug:740861064257863690>" to just ":CS_Shrug:" i know this has something with discord displaying stuff but is there anyway to get around this? People asked for code:
let args = msg.content.slice(PREFIX.length).trim().split(/ +/); //slice message in an array
if (args[1]) { //if theres something mentioned
let newRawdata = fs.readFileSync('./emojis.json');
let newData = JSON.parse(newRawdata);
var findEmoji = Object.values(newData).find(object => object === args[1]) //just a file with all unicode emojis bc yes
if (args[1].startsWith("<:") && args[1].endsWith(">")) { //custom server emoji
//not important code
} else if (args[1].startsWith("<a:") && args[1].endsWith(">")){
let ForBots = args[1]
let namever1 = ForBots.replace('<a','')
let namever2 = namever1.replace('>','')
let namever3 = namever2.split(':')
let name = namever3[1]
let id = namever3[2]
var inforEmbed2 = new Discord.MessageEmbed()
.setColor('#2ab94c')
.setTitle('Emoji information')
.addFields(
{ name: 'Type', value: "`Animated server emoji`", inline: false },
{ name: 'Name', value: "`"+name+"`", inline: false },
{ name: 'ID', value: "`"+id+"`", inline: true },
{ name: 'For bots', value: "`"+args[1]+"`", inline: true }, //should display `<a:CS_Shrug:740861064257863690>` but instead `:CS_Shrug:`
{ name: 'Emoji link', value: "https://cdn.discordapp.com/emojis/"+id+".gif?/v=1", inline: false },
)
.setThumbnail('https://cdn.discordapp.com/emojis/'+id+'.gif?/v=1')
.setFooter("C.U.A.L - Bot| ;help", 'https://cdn.discordapp.com/attachments/864287060424785941/872600493082415124/Avatar_1.png')
msg.channel.send(inforEmbed2)
} else if (findEmoji) { //default emoji
//more none important code
} else msg.reply("donno wtf this is") // wasnt an emoji
} else msg.reply("Gotta give me something first")
When someone adds an emoji to the server, the bot will log it. I'm trying to make it where it will but the emoji in the thumbnail of the log embed. Whenever it trys to log, I get the error..
[15:55:28] [UTC June:10:2021] [ERROR] (node:11176) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.thumbnail.url: Scheme "undefined" is not supported. Scheme must be one of ('http', 'https'). embeds[0].thumbnail.url: Scheme "undefined" is not supported. Scheme must be one of ('http', 'https').
I'm guessing that the code for the thumbnail is returned undefined and you can't put that into a thumbnail. My code is
const Discord = require("discord.js")
exports.run = async(client) => {
client.on("emojiCreate", function(emoji){
console.log(emoji)
const Embedf = new Discord.MessageEmbed()
.setColor("#1CDEA3")
.setTitle('New Emoji Created')
.addFields(
{ name: 'Emoji Name:', value: emoji.name, inline: false },
{ name: 'Emoji ID:', value: emoji.id, inline: false },
{ name: 'Nitro Emote?', value: emoji.animated, inline: false })
.setThumbnail(`${emoji.icon}`)
.setFooter(`${emoji.guild.name}`)
.setTimestamp()
client.channels.cache.find(channel => channel.id == "851987273837969408").send(Embedf)
})
}
How can I fix this? Thanks
A GuildEmoji does not have an icon property.
You may have meant to use the url property.
const Embedf = new Discord.MessageEmbed()
.setColor("#1CDEA3")
.setTitle('New Emoji Created')
.addFields(
{ name: 'Emoji Name:', value: emoji.name, inline: false },
{ name: 'Emoji ID:', value: emoji.id, inline: false },
{ name: 'Nitro Emote?', value: emoji.animated, inline: false })
.setThumbnail(emoji.url) // use URL instead of icon
.setFooter(emoji.guild.name)
.setTimestamp()
I want to have my status be "Watching" for command -help", but I don't know how to switch it from playing to watching,
Here is my code:
command(client, 'status', (message) => {
if (message.author.id != "my id goes here") return;
const content = message.content.replace('-status ', '')
client.user.setPresence({
activity: {
name: content,
type: 0,
},
})
})
You need to set the code :
client.user.setPresence({
status: 'online', //you can set online , idle , dnd and offline
activity: {
name: 'Some Name',
type: 'PLAYING', //here you can change it to 'WATCHING' , 'PLAYING' , STREAM,
url: 'https://discord.com'
}
});
For more info you can check here
I've been having trouble trying to get my discord bot to send a randomised message. For some reason, my code doesn't work. When I try to run it, it says "message is not defined." I'm pretty new to coding, and the answer is probably very simple. Here is my code for the randomMessage part:
const messages = ['message one', 'message two', 'message three', 'message four'];
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
This is the command for when someone says (prefix) random, it will then basically go to the file named "random".
...
else if(command === 'random')
client.commands.get('random').execute(message, args);
...
and on that file is the name of the command, the description and it also tells the bot to send (randomMessage), which should pick either message one, two, three or four?
module.exports= {
name: 'random',
description: 'send a randomised message.',
execute(message, args){
message.channel.send(randomMessage)
}
}
if anyone knows my mistake, I'd love to hear it. Thank you!
You need to put the code in the random command file:
module.exports = {
name: 'random',
description: 'send a randomised message.',
execute(message, args) {
const messages = ['message one', 'message two', 'message three', 'message four'];
const randomMessage = messages[Math.floor(Math.random() * (messages.length - 1))];
message.channel.send(randomMessage);
}
}
If you want the randomised message to be decided on startup and not change while the bot is active, you can put the code at the top of the file:
const messages = ['message one', 'message two', 'message three', 'message four'];
const randomMessage = messages[Math.floor(Math.random() * (messages.length - 1))];
module.exports = {
name: 'random',
description: 'send a randomised message.',
execute(message, args) {
message.channel.send(randomMessage);
}
}