How to log deleted images with a discord bot - node.js

I have been spending hours trying to get this code to log deleted images but everything I have tried its been nothing but fails, it logs deleted messages but images it just ignores completely.
Can someone please point me in the right direction on how to fix this issue, please. Any help is very much appreciated
const Discord = require('discord.js')
module.exports = async (client, message) => {
if (!message.guild) return;
if (!message.content) return;
const logs = message.guild.channels.find(c => c.name === '420-logs');
if (!logs) {
return console.log(`[WARN]: The Delete Logs channel does not exist in the server named '${message.guild.name}'`)
}
if (message.attachments.size > 0) { // If I change this to: message.attachments.size>0 && message it works with deleted image & text but as it is without this said line it doesn't function
var Attachment = (message.attachments).array();
Attachment.forEach(function(attachment) {
const logembed = new Discord.RichEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL)
.setDescription(`**Image sent by ${message.author.tag} deleted in <#${message.channel.id}>**`)
.setImage(attachment.proxyURL)
.setColor(message.guild.member(client.user).displayHexColor)
.setFooter(`Deleted Image`)
.setTimestamp()
logs.send(logembed);
console.log(attachment.proxyURL);
})
} else {
const logembed = new Discord.RichEmbed()
//.setTitle('Message Deleted')
.setAuthor(message.author.tag, message.author.displayAvatarURL)
.setDescription(`**Message sent by ${message.author.tag} deleted in <#${message.channel.id}>**`)
.addField("Message Content", `${message.content}`)
.setColor(message.guild.member(client.user).displayHexColor)
.setFooter(`Deleted Message`)
.setTimestamp()
logs.send(logembed);
}
}

So far you can't. When a member deletes a message, sure it does return all the stuff for a message with attachment links and all, but when that image is deleted the URL returned is useless because it references an image that doesn't exist on the Discord servers. If you open the link in a web browser it will say this:

Related

getting weird mongodb object on client

well as i mentioned in the title when i'm sending message through socket to the server then save it in database mongodb with mongoose, then i returned the the message and send it back in the socket,
now when i tried to print it on console in the server right before i send it to the client with socket,
i got the object i wanted to send, but when i checked the object i got in the client, then i got diffrent object seems related to mongo probably(pretty sure) and i'm not sure what should i do to fix it.this is what i get in the server right before i send it back to the client
this what i get in the client when i recieve new message
const addMessage = async (newMessage) => {
try {
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.message = Buffer.from(newMsg.message.buffer, "base64")
return newMsg;
} catch (error) {
console.log(error);
errorHandler(error);
}
};
i expected to get the same object i see in the server so in the client too
you should always, provide code snippets while asking a question.
Th probable reason for the above problem is, you are printing the console.log(result._docs) and sending to the databse the complete result object. Try sending result._docs to your database as well.
P.S: result is just a variable to which your assigning the data. This variable may be different in your code.
(If this does not work, edit your question and attach code)
well I found the problem, though i'm not sure how to describe it but the problem was here:
const newMsg = new Message(newMessage);
await newMsg.save();
newMsg.type === 2
? Buffer.from(newMsg.message.buffer, "base64")
: newMsg.message;
return newMsg;
so i took the newMessage and inserted newMsg.toJSON() to it;
if (newMessage.type === 2) {
const audioBlob = new Buffer.from(newMessage.message).toString("base64");
newMessage.message = new Binary(audioBlob, Binary.SUBTYPE_BYTE_ARRAY);
}
const newMsg = new Message(newMessage);
await newMsg.save();
newMessage = newMsg.toJSON();
if (newMessage.type === 2) {
newMessage.message = Buffer.from(newMessage.message.buffer, "base64");
}
return newMessage;
and now it's working!

How to send a message if user has the permission

Okay so, im trying to make an announce command that makes the bot say what the user asks it to write,
the command is working fine but the main problem is that i dont want users that are not moderators/admins to use this command i tried to use if (user.hasPermission("KICK_MEMBERS") but i simply don't know how to implement this into my code, (here it is)
const Discord = require('discord.js');
module.exports = {
name: 'announce',
description: "announce",
execute(message, args){
const developerEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle(`failed to send message`)
.setAuthor('♥Aiko♥', 'https://i.imgur.com/1q9zMpX.png')
.setDescription('please mention the channel first(if this promblem persists contact the developer)')
.setTimestamp()
.setFooter(`©Revenge#7005 | Requested by ${message.author.tag}.`);
if(message.mentions.channels.size === 0) {
message.channel.send(developerEmbed);
}
else {
let targetChannel = message.mentions.channels.first();
const args = message.content.split(" ").slice(2);
let userMessage = args.join(" ");
targetChannel.send(userMessage);
message.delete();
}
}
}
so yeah, any ideas how to make the bot check for the permission and then send the message if the user has it?
i'm pretty new to coding and this bot is my first bigger project so sorry if this question seems stupid
Make sure to use message.member in place of user, and you should implement it at the beginning of your code.
module.exports = {
name: 'announce',
description: "announce",
execute(message, args) {
if (!message.member.hasPermisson('KICK_MEMBERS')) // if member doesn't have permissions
return message.channel.send('Insufficient Permissions');
// rest of your code...

Discord.js: Verify channel

I'm trying to make my own bot for my server, for now i'm focusing on the verify. By acting to the check mark emoji it'll add the verified role, and then it should remove only the user reaction, but instead it'll remove every reaction right away
client.on('messageReactionAdd', async (reactionReaction, user) => {
const message = reactionReaction.message;
const verifyChannel = message.guild.channels.cache.find(c => c.name === 'approvazione');
const member = message.guild.members.cache.get(user.id);
if (member.user.bot) return;
const verify = message.guild.roles.cache.get('728000975046180988');
if (reactionReaction.emoji.name === '✅' && message.channel.id === verifyChannel.id) {
member.roles.add(verify).catch(console.error);
await reactionReaction.remove(member).catch(console.error);
}
here is the message sent by the bot with it's own reaction
and here is the same message after i reacted, and both mine and the bot reaction are removed, i just want my reaction to be removed
If you look at the docs it takes no parameter for the user:
https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=remove
This was changed in v12, the method now is to use .users.remove:
reactionReaction.users.remove(member);

Moderation Log Message Discord JS

Hello StackOverflow community, this is my first question in need help with for Discord, could someone please edit this code to made this command send a log to #logs channel with the following info
1) Title - Moderation Log
2) User the moderation action was performed on
3) What moderation action was performed (in this case ban)
4) Moderator
5) Footer - Proton Servers Bot©2020
const { client, config, functions } = require('../index.js');
module.exports.run = async (message, u) => {
if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send(functions.errorEmbed('You are not permitted to do this.'));
if (!u) return message.channel.send(functions.usageEmbed(config.prefix + 'ban <user/id>'));
const userID = u.match(/[0-9]+/) ? u.match(/([0-9]+)/)[0] : undefined;
const user = message.guild.members.get(userID);
if (!user) return message.channel.send(functions.errorEmbed('**' + u + '** is not a valid user.'));
try {
await user.ban();
return message.channel.send(functions.successEmbed('Banned ' + user + ' successfully.'));
} catch {
return message.channel.send(functions.errorEmbed('Could not ban ' + user + '. Is their role higher than mine?'));
}
}
module.exports.description = 'Ban a user.';```
This answer is for discord.js v11. If you want to do this using discord.js v12 then use MessageEmbed instead of RichEmbed and use cache to get the channels collection message.guild.channels.cache
You could use a RichEmbed to create the embed you want and then send it to the log channel.
First, though, you have to get the log channel, there are 2 ways of doing this:
By name: const logChannel = message.guild.channels.find(channel => channel.name === 'log')
Or by ID: const logChannel = message.guild.channels.get(CHANNEL_ID)
Then you could use that to send the following embed
const embed = new Discord.RichEmbed() // Or new RichEmbed depending on how you're importing things
.setTitle('Moderation Log')
.addField('Banned', user)
.addField('Banned By', message.author)
.setFooter('Proton Servers Bot©2020')
logChannel.send(embed)

(Discord.js) how too tag the bot itself

okay , hey there.
Im trying too ping/tag the bot itself in Discord.js.
soo when a user ping the bot in a command that it replies with an error message i defined myself.
thats what i have so far :
async run(message, args)
{
let huggeduser = message.mentions.users.first()
let auser = message.author
if(huggeduser == message.author) {
message.channel.send(`${auser} , you cant hug yourself!`)
return;
}
if(client.bot = huggeduser) { //(Note) How too tag the bot as it self like : (huggeduser == <the string too tag the bot>) {
message.channel.send("You cant meeeee hug!")
return;
}
the rest of the code works fine.
just the code-part of the with the (note)
i would love if someone can help me on that. ive been trying it myself for 2 days now.
Thanks.
Use double equal signs for checking equality. Also, checking the entire user objects is inefficient, just check their IDs.
Additionally, based on your error in the comments, and the name of this function, I'm assuming this is in a different file and you have a line like this at the top:
const client = new Discord.Client();
So in this context, client is not your logged-in bot user, it's an empty, not logged in bot that doesn't know who it is. You should get the client from the message object:
async run(message, args)
{
let huggeduser = message.mentions.users.first()
let auser = message.author
if(!huggeduser) return; //message for if no user was mentioned?
if(huggeduser.id == auser.id) {
message.channel.send(`${auser} , you cant hug yourself!`)
return;
} else if(huggeduser.id == message.client.user.id) {
message.channel.send("You cant meeeee hug!")
return;
}
//rest of code

Resources