From 74d0379885c894a30ece7da48fdea29c61201df3 Mon Sep 17 00:00:00 2001 From: Lio Young Date: Sun, 17 Oct 2021 18:39:40 +0200 Subject: [PATCH] first commands n'stuff --- src/discord/commands/testing.ts | 19 ++++++++ src/discord/events/message.ts | 78 +++++++++++++++++++++++++++++++++ src/discord/events/ready.ts | 7 +++ 3 files changed, 104 insertions(+) create mode 100644 src/discord/commands/testing.ts create mode 100644 src/discord/events/message.ts create mode 100644 src/discord/events/ready.ts diff --git a/src/discord/commands/testing.ts b/src/discord/commands/testing.ts new file mode 100644 index 0000000..fc8fd12 --- /dev/null +++ b/src/discord/commands/testing.ts @@ -0,0 +1,19 @@ +import { Context } from "../../utils/types" +import { Command } from "../../modules/eu/src/index" + +export = class TestingCommand extends Command { + constructor() { + super({ + name: "testing", + aliases: ["t"], + }) + // console.log(this) + } + + async run(context: Context): Promise { + // console.log(context) + // console.log(this.nsfw) + console.log("testing") + return "Generic command" + } +} diff --git a/src/discord/events/message.ts b/src/discord/events/message.ts new file mode 100644 index 0000000..541551b --- /dev/null +++ b/src/discord/events/message.ts @@ -0,0 +1,78 @@ +import { Message, Collection, TextChannel, MessageEmbed } from 'discord.js'; +import { Context } from '../../utils/types'; +import { EuClient } from '../../modules/eu/src/misc/types'; +import modulus from '../../utils/modulus' +import prefixHandler from '../../utils/prefix' +import config from '../../utils/config'; +import language from '../../utils/language'; +import replace from '../../utils/replace'; +export = { + name: "messageCreate", + run: async (Eu: EuClient, message: Message) => { + if (message.author.bot) return; + + let helper = await prefixHandler(message.guild.id, message.content) + if (!helper.success) return + + // @ts-ignore + const cmd = Eu.commands.find((c) => c.name == helper.command || (c.aliases && c.aliases.includes(helper.command))) + if (!cmd) return + + if (!Eu.cooldowns.has(cmd.name)) { + Eu.cooldowns.set(cmd.name, new Collection()) + } + + let ctx: Context = { + Eu, message, modulus, config, + args: helper.args, + channel: (message.channel as TextChannel), + guild: message.guild, + member: message.member, + author: message.author, + settings: await modulus.server(message.guild.id), + // @ts-ignore + isDev: config?.devs.find(d => d == message.author.id) + + } + + if (ctx.isDev) cmd.permissions = ["NONE"] + if (cmd.permissions !== [] && ctx.member.permissions.toArray().some(p => cmd.permissions.includes(p))) + return ctx.channel.send(replace(/PERMISSIONS/gm, cmd.permissions.join(', '), language.get(ctx.settings.locale).error.permissions)) + + if (cmd.nsfw && !ctx.channel.nsfw) return ctx.channel.send(language.get(ctx.settings.locale).error.nsfw) + + const now = Date.now() + const timestamps = Eu.cooldowns.get(cmd.name) + const cooldown = (cmd.cooldown || 1) * 1000 + // @ts-ignore + if (timestamps.has(ctx.author.id)) { + // @ts-ignore + const time = timestamps.get(ctx.author.id) + cooldown + if (now < time) { + const left = ((time - now) / 1000).toFixed(1) + let embed = new MessageEmbed() + .setTitle(replace(/COMMAND/gm, cmd.name, language.get(ctx.settings.locale).error.cooldown.name)) + // @ts-ignore + .setDescription(replace(/COMMAND/gm, cmd.name, replace(/COOLDOWN/gm, `${cmd.cooldown}s`, replace(/TIME/g, left, language.get(ctx.settings.locale).error.cooldown.desc)))) + .setColor("RED") + + // @ts-ignore + return message.channel.send({ embeds: [embed] }) + } + } else { + // @ts-ignore + timestamps.set(ctx.author.id, now) + setTimeout(() => { + // @ts-ignore + timestamps.delete(ctx.author.id, now) + }, cooldown); + try { + await cmd.run(ctx) + } catch (error) { + + let ErrorEmbed = new MessageEmbed().setTitle(replace(/COMMAND/g, cmd.name, language.get(ctx.settings.locale).error.error)).setDescription(`\`${error.message}\`\n\n\`${error}\``).setColor("RED") + return message.channel.send({ embeds: [ErrorEmbed] }) + } + } + } +}; \ No newline at end of file diff --git a/src/discord/events/ready.ts b/src/discord/events/ready.ts new file mode 100644 index 0000000..e060ffa --- /dev/null +++ b/src/discord/events/ready.ts @@ -0,0 +1,7 @@ + +export = { + name: "ready", + run: async (Eu: any) => { + console.log(`${Eu.user.username} is ready!`); + } +}; \ No newline at end of file