Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
editsnipe and reactionsnipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Cats3153 committed Sep 30, 2021
1 parent 5e36ff4 commit 0f33210
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ dist
# TernJS port file
.tern-port

# IDE config
.idea/

# other
.DS_Store
config.json
30 changes: 30 additions & 0 deletions scripts/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ const commands = [
{
name: "snipe",
description: "Shows the last deleted message from a specified channel!",
options: [
{
type: 7, // text channel
name: "channel",
description: "The channel to snipe",
},
],
},
{
name: "editsnipe",
description: "Shows the last edited message from a specified channel!",
options: [
{
type: 7, // text channel
name: "channel",
description: "The channel to snipe",
},
],
},
{
name: "reactionsnipe",
description:
"Shows the last removed reaction from a specified channel!",
options: [
{
type: 7, // text channel
name: "channel",
description: "The channel to snipe",
},
],
},
];

Expand Down
86 changes: 82 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,83 @@
const { Client, Intents, MessageEmbed } = require("discord.js");
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
partials: ["MESSAGE", "REACTION", "USER"],
});
const { token } = require("../config.json");

const snipes = {};
const editSnipes = {};
const reactionSnipes = {};

const formatEmoji = (emoji) => {
return !emoji.id || emoji.available
? emoji.toString() // bot has access or unicode emoji
: `[:${emoji.name}:](${emoji.url})`; // bot cannot use the emoji
};

client.on("ready", () => {
console.log(`[sniper] :: Logged in as ${client.user.tag}.`);
});

client.on("messageDelete", async (message) => {
snipes[message.guild.id] = {
if (message.partial) return; // content is null

snipes[message.channel.id] = {
author: message.author,
content: message.content,
createdAt: message.createdTimestamp,
};
});

client.on("messageUpdate", async (oldMessage, newMessage) => {
if (oldMessage.partial) return; // content is null

editSnipes[oldMessage.channel.id] = {
author: oldMessage.author,
content: oldMessage.content,
createdAt: newMessage.editedTimestamp,
};
});

client.on("messageReactionRemove", async (reaction, user) => {
if (reaction.partial) reaction = await reaction.fetch();

reactionSnipes[reaction.message.channel.id] = {
user: user,
emoji: reaction.emoji,
messageURL: reaction.message.url,
createdAt: Date.now(),
};
});

client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand() && interaction.commandName === "snipe") {
const snipe = snipes[interaction.guildId];
if (!interaction.isCommand()) return;

const channel =
interaction.options.getChannel("channel") || interaction.channel;

if (interaction.commandName === "snipe") {
const snipe = snipes[channel.id];

await interaction.reply(
snipe
? {
embeds: [
new MessageEmbed()
.setDescription(snipe.content)
.setAuthor(snipe.author.tag)
.setFooter(`#${channel.name}`)
.setTimestamp(snipe.createdAt),
],
}
: "There's nothing to snipe!"
);
} else if (interaction.commandName === "editsnipe") {
const snipe = editSnipes[channel.id];

await interaction.reply(
snipe
Expand All @@ -29,6 +86,27 @@ client.on("interactionCreate", async (interaction) => {
new MessageEmbed()
.setDescription(snipe.content)
.setAuthor(snipe.author.tag)
.setFooter(`#${channel.name}`)
.setTimestamp(snipe.createdAt),
],
}
: "There's nothing to snipe!"
);
} else if (interaction.commandName === "reactionsnipe") {
const snipe = reactionSnipes[channel.id];

await interaction.reply(
snipe
? {
embeds: [
new MessageEmbed()
.setDescription(
`reacted with ${formatEmoji(
snipe.emoji
)} on [this message](${snipe.messageURL})`
)
.setAuthor(snipe.user.tag)
.setFooter(`#${channel.name}`)
.setTimestamp(snipe.createdAt),
],
}
Expand Down

0 comments on commit 0f33210

Please sign in to comment.