added eslint typescript

This commit is contained in:
Xeovalyte 2023-06-28 14:19:07 +02:00
parent d52431ffb5
commit fbc9238e97
11 changed files with 1890 additions and 30 deletions

17
discordbot/.eslintrc.json Normal file
View File

@ -0,0 +1,17 @@
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"root": true,
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module"
},
"rules": {
"no-console": "off"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,9 @@
"scripts": { "scripts": {
"dev": "tsx watch src/index.ts", "dev": "tsx watch src/index.ts",
"start": "node dist/index.js", "start": "node dist/index.js",
"build": "tsup src/index.ts --minify" "build": "tsup src/index.ts --minify",
"lint": "eslint \"**/*.{ts, tsx}\"",
"eslint-fix": "eslint . --fix"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -15,6 +17,9 @@
"dotenv": "^16.3.1" "dotenv": "^16.3.1"
}, },
"devDependencies": { "devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"eslint": "^8.43.0",
"tsup": "^7.1.0", "tsup": "^7.1.0",
"tsx": "^3.12.7", "tsx": "^3.12.7",
"typescript": "^5.1.3" "typescript": "^5.1.3"

View File

@ -0,0 +1,24 @@
import { ChatInputCommandInteraction, SlashCommandBuilder, Client, PermissionFlagsBits } from "discord.js";
import { basicEmbed } from '../createEmbed'
export const data = new SlashCommandBuilder()
.setName("clear")
.setDescription("Clear 1-100 messages")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addNumberOption(option => option
.setName('amount')
.setDescription('The amount of messages to creat')
.setRequired(true)
)
export async function execute({ interaction }: { interaction: ChatInputCommandInteraction, client: Client }) {
const amount = interaction.options.getNumber('amount');
if (!amount || !interaction.channel || amount < 1 || amount > 100) return await interaction.reply({ embeds: [basicEmbed('The amount must be between 1-100')] });
if (interaction.channel.isDMBased()) return await interaction.reply({ embeds: [basicEmbed('This command can only be executed inside a guild')], ephemeral: true })
interaction.channel.bulkDelete(amount)
await interaction.reply({ embeds: [basicEmbed(`Cleared **${amount}** messages`)], ephemeral: true })
}

View File

@ -1,10 +1,11 @@
import { CommandInteraction, SlashCommandBuilder, Client } from "discord.js"; import { ChatInputCommandInteraction, SlashCommandBuilder, Client } from "discord.js";
import { basicEmbed } from '../createEmbed'
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName("ping") .setName("ping")
.setDescription("Replies with Pong!"); .setDescription("Replies with Pong!");
export async function execute({ interaction, client, createEmbed }: { interaction: CommandInteraction, client: Client, createEmbed: Function }) { export async function execute({ interaction, client }: { interaction: ChatInputCommandInteraction, client: Client }) {
const reply = await interaction.reply({ embeds: [createEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **Pinging...**`)], fetchReply: true, ephemeral: true }); const reply = await interaction.reply({ embeds: [basicEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **Pinging...**`)], fetchReply: true, ephemeral: true });
interaction.editReply({ embeds: [createEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **${reply.createdTimestamp - interaction.createdTimestamp}ms**`)] }); interaction.editReply({ embeds: [basicEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **${reply.createdTimestamp - interaction.createdTimestamp}ms**`)] });
} }

View File

@ -0,0 +1,8 @@
import { EmbedBuilder } from 'discord.js'
import { config } from './config'
export const basicEmbed = (description: string) => {
return new EmbedBuilder()
.setColor(config.EMBED_COLOR)
.setDescription(description);
};

View File

@ -2,7 +2,7 @@ import { Events, Client, BaseInteraction } from 'discord.js'
export const name = Events.InteractionCreate export const name = Events.InteractionCreate
export const execute = async ({ client, createEmbed }: { client: Client, createEmbed: Function }, interaction: BaseInteraction) => { export const execute = async ({ client }: { client: Client }, interaction: BaseInteraction) => {
if (interaction.isChatInputCommand()) { if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName); const command = interaction.client.commands.get(interaction.commandName);
@ -13,7 +13,7 @@ export const execute = async ({ client, createEmbed }: { client: Client, createE
} }
try { try {
await command.execute({ interaction, client, createEmbed }); await command.execute({ interaction, client });
} catch (error) { } catch (error) {
console.error(error); console.error(error);

View File

@ -6,6 +6,8 @@ export const name = Events.ClientReady
export const once = true export const once = true
export const execute = async ({ client }: { client: Client }) => { export const execute = async ({ client }: { client: Client }) => {
if (!client.user) throw Error('No client.user')
console.log(`Ready! Logged in as ${client.user.tag}`) console.log(`Ready! Logged in as ${client.user.tag}`)
registerCommands() registerCommands()

View File

@ -1,45 +1,48 @@
import { Client, GatewayIntentBits, Collection, EmbedBuilder } from 'discord.js' import { Client, GatewayIntentBits, Collection } from 'discord.js'
import fs from 'node:fs' import fs from 'node:fs'
import path from 'node:path' import path from 'node:path'
import { config } from './config' import { config } from './config'
type Command = {
data: any
}
const client = new Client({ const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
}) })
const createEmbed = (description: string) => {
return new EmbedBuilder()
.setColor(config.EMBED_COLOR)
.setDescription(description);
};
client.commands = new Collection() client.commands = new Collection()
const commandsPath = path.join(__dirname, 'commands'); const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts')); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) { (async () => {
const filePath = path.join(commandsPath, file); for (const file of commandFiles) {
const command = require(filePath); const filePath = path.join(commandsPath, file);
const command = await import(filePath) as Command
if ('data' in command && 'execute' in command) { if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
} else { } else {
console.error(`The command at ${filePath} is missing a required "data" or "execute" property.`); console.error(`The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} }
} })
const eventsPath = path.join(__dirname, 'events'); const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.ts')); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.ts'));
for (const file of eventFiles) { (async () => {
const filePath = path.join(eventsPath, file); for (const file of eventFiles) {
const event = require(filePath); const filePath = path.join(eventsPath, file);
if (event.once) { const event = await import(filePath);
client.once(event.name, (...args) => event.execute({ client, createEmbed }, ...args)); if (event.once) {
} else { client.once(event.name, (...args) => event.execute({ client }, ...args));
client.on(event.name, (...args) => event.execute({ client, createEmbed }, ...args)); } else {
client.on(event.name, (...args) => event.execute({ client }, ...args));
}
} }
} })
client.login(config.DISCORD_TOKEN); client.login(config.DISCORD_TOKEN);

View File

@ -10,6 +10,9 @@
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"strictNullChecks": true, "strictNullChecks": true,
"skipLibCheck": true "skipLibCheck": true,
"compilerOptions": {
"module": "commonjs"
}
} }
} }

View File

@ -5,3 +5,7 @@ declare module 'discord.js' {
commands: Collection<unknown, any> commands: Collection<unknown, any>
} }
} }
declare global {
type CreateEmbed = (message: string) => void
}