Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: message events #17

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { container } from "@sapphire/pieces";
import type { Message, TextChannel } from "discord.js";

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 emojiHelper from "@/events/emojiHelper.js";
import giveXp from "@/events/giveXp.js";
import urlHelper from "@/events/urlHelper.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);
}
}