Skip to content

Commit

Permalink
feat: message events
Browse files Browse the repository at this point in the history
Co-authored-by: Psykka <Psykka@users.noreply.github.com>
  • Loading branch information
N3aar and Psykka committed Jun 2, 2024
1 parent 2a1de37 commit 9f2a2b4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TODO:
- [ ] Remind Roll Command / Notify after 15 Minutes
- [x] Utilities Commands
- [x] Color with Roles
- [x] Emoji fix (send webhook with content menssage and replace the emoj if the user does't have nitro)

Anime Component:
- [ ] Anilist Command
Expand Down
49 changes: 49 additions & 0 deletions src/events/emojiHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { TextChannel, Message } from "discord.js";
import { container } from "@sapphire/pieces";

export default async function emojiHelper(message: Message) {
const clientEmojis = container.client.emojis.cache;
const emotes = clientEmojis?.map((emoji) => emoji.toString());

if (!emotes) return;

const regex = /(?<!<\w?):(\w+):(?!\d+>)/g;
const content = message.content;
const catchedEmojis = content.match(regex);

if (
!catchedEmojis?.length ||
catchedEmojis?.every((emoji) =>
clientEmojis.every((emojiData) => `:${emojiData.name}:` !== emoji),
)
)
return;

const fixed = message.content.replace(regex, (match, name) => {
if (emotes.includes(match)) return match;
return (
clientEmojis.find((emojiData) => emojiData.name === name)?.toString() ||
""
);
});

if (fixed.length) {
if (message.deletable) {
message.delete();
}

const channel = message.channel as TextChannel;
const member = message.member;

if (member) {
const webhook = await channel.createWebhook({
name: member?.displayName ?? member.user.username,
avatar: member.displayAvatarURL({ forceStatic: false }),
reason: "Emoji Helper!",
});

await webhook.send(fixed);
webhook.delete();
}
}
}
41 changes: 41 additions & 0 deletions src/events/urlHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Message } from "discord.js";

const sites = {
"https://www.youtube.com/shorts/": "https://youtu.be/",
"https://www.youtube.com/watch?v=": "https://youtu.be/",
"https://twitter.com/": "https://vxtwitter.com/",
"https://x.com/": "https://vxtwitter.com/",
"https://www.tiktok.com/": "https://www.vxtiktok.com/",
"https://www.instagram.com/": "https://g.ddinstagram.com/",
"https://www.reddit.com/": "https://rxddit.com/",
};

export default function urlHelper(message: Message) {
const content = message.content;
const regex = /(https?:\/\/[^\s]+)/g;
const urls = content.match(regex);
const fixes = Object.entries(sites);

if (
!urls ||
!urls.length ||
urls.every((url) => fixes.every(([site]) => !url.includes(site)))
)
return;

const fixedUrls = urls.map((url) => {
let newUrl = url;
for (const [site, replacement] of fixes) {
if (newUrl.includes(site)) {
newUrl = newUrl.replace(site, replacement);
}
}
return newUrl;
});

message.suppressEmbeds(true);
message.reply({
content: fixedUrls.join("\n"),
allowedMentions: { repliedUser: false },
});
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const client = new Client({
GatewayIntentBits.GuildMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildEmojisAndStickers,
],
loadMessageCommandListeners: true,
});
Expand Down
25 changes: 25 additions & 0 deletions src/listeners/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import giveXp from "@/events/giveXp.js";
import urlHelper from "@/events/urlHelper.js";
import emojiHelper from "@/events/emojiHelper.js";
import { Listener } from "@sapphire/framework";
import type { Message } from "discord.js";

export class MessageCreate extends Listener {
public constructor(
context: Listener.LoaderContext,
options: Listener.Options,
) {
super(context, {
...options,
event: "messageCreate",
});
}

public run(message: Message) {
if (message.author.bot) return;

urlHelper(message);
emojiHelper(message);
giveXp(message);
}
}

0 comments on commit 9f2a2b4

Please sign in to comment.