This commit is contained in:
Amaryllis 2023-11-30 21:17:29 +01:00
parent d4f2acaae6
commit 0ee296033c
5 changed files with 839 additions and 428 deletions

49
bot.ts
View file

@ -1,48 +1,29 @@
import { Client, GatewayIntentBits, GuildMember, Channel, TextBasedChannel, Events, Message, SlashCommandBuilder, Collection } from 'discord.js';
import * as dotenv from 'dotenv'; import * as dotenv from 'dotenv';
dotenv.config(); dotenv.config();
import config from "./config"; import config from "./config";
import { Socket } from 'detritus-client-socket/lib/gateway'; export const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
import { Client } from "detritus-client-rest";
import * as types from 'discord-api-types/v10';
const socket = new Socket(process.env.token!, { intents: 33283 });
export const restClient = new Client(process.env.token);
import handleMessage from './messageHandler'; import handleMessage from './messageHandler';
import help from './command/help';
socket.on('packet', async (evt: types.GatewayDispatchPayload) => handleEvtInner(evt).catch(e => console.error(e, JSON.stringify(e, null, 2)))); client.on("ready", () => {
console.log("successfully logged in");
});
async function handleEvtInner(evt: types.GatewayDispatchPayload) { client.on("guildMemberAdd", async (member: GuildMember) => {
if (evt.op != 0) return; const msg = `Welcome, <@${member.id}>! If you joined for any specific support questions `
if (evt.t == 'READY') {
console.log("successfully logged in:", evt.d.user);
}
// i am quite sure this is the correct type
// @ts-expect-error
if (evt.t == 'INTERACTION_CREATE') await help.interactionHandler(evt.d);
if (evt.t == 'GUILD_MEMBER_ADD') {
if (evt.d.guild_id != config.guild) return;
if (evt.d.user!.bot) return;
const msg = `Welcome, <@${evt.d.user!.id}>! If you joined for any specific support questions `
+ `please check out <#863171642905591830> first to see if your issue is known, ` + `please check out <#863171642905591830> first to see if your issue is known, `
+ `and make sure that your app is up-to-date before posting.`; + `and make sure that your app is up-to-date before posting.`;
await restClient.createMessage(config.channels.joins, { content: msg });
}
if (evt.t == 'MESSAGE_CREATE') { const channel: TextBasedChannel | null = await client.channels.fetch(config.channels.joins) as TextBasedChannel;
if (evt.d.guild_id != config.guild) return; channel?.send(msg);
if (evt.d.author.bot) return; });
await handleMessage(evt.d).catch(console.error);
}
};
console.log('Logging in...'); client.on(Events.MessageCreate, async (msg: Message) => {
socket.connect("https://gateway.discord.gg"); await handleMessage(msg).catch(console.error);
});
client.login(process.env.token);

View file

@ -1,35 +1,10 @@
import * as types from 'discord-api-types/v10'; import { client } from './bot';
import CommandCategory from './model/command-category';
import { restClient } from './bot';
import config from './config'; import config from './config';
import avatar from './command/avatar';
import changelog from './command/changelog';
import clean from './command/clean';
import eval from './command/eval';
import help from './command/help';
import ticket from './command/ticket';
import * as customMessages from './model/messages'; import * as customMessages from './model/messages';
import { Message, TextBasedChannel } from 'discord.js';
export interface Command {
aliases: string[];
category: CommandCategory;
isAllowedForContext(_: types.GatewayMessageCreateDispatchData): boolean;
description?: string;
process(_: types.GatewayMessageCreateDispatchData, __: string[]): Promise<void>;
}
const privCommands: Record<string, Command> = {};
export const commands = [avatar, changelog, clean, eval, help, ticket];
commands.map((x: Command) => { export default async function handleMessage(message: Message) {
x.aliases.map(a => privCommands[a] = x);
});
export default async function handleMessage(message: types.GatewayMessageCreateDispatchData) {
if (!message.content.toLowerCase().startsWith(config.prefix)) if (!message.content.toLowerCase().startsWith(config.prefix))
return false; return false;
@ -37,10 +12,14 @@ export default async function handleMessage(message: types.GatewayMessageCreateD
const calledCommand = args.shift()!.toLowerCase(); const calledCommand = args.shift()!.toLowerCase();
const embed = customMessages.getEmbed(calledCommand); const embed = customMessages.getEmbed(calledCommand);
if (embed != null) if (embed != null) {
return await restClient.createMessage(message.channel_id, { embed }); const channel: TextBasedChannel | null = await client.channels.cache.get(message.channelId) as TextBasedChannel
channel.send({
const foundCommand = privCommands[calledCommand]; embeds: [{
if (foundCommand == null || !foundCommand.isAllowedForContext(message)) return; color: embed.color,
await foundCommand.process(message, args); title: embed.author.name,
description: embed.description
}]
});
}
} }

View file

@ -1,4 +1,4 @@
import { restClient } from "../bot"; import { client } from "../bot";
interface Message { interface Message {
names: string[]; names: string[];
@ -12,15 +12,17 @@ interface Message {
export const messages: Record<string, Message> = {}; export const messages: Record<string, Message> = {};
let avatarUrl = ""; let avatarUrl = "";
const load = async () => const load = async () => {
{
const messagesList = require('../messages.json')["faq"]; const messagesList = require('../messages.json')["faq"];
messagesList.forEach((msg: any) => { messagesList.forEach((msg: any) => {
messages[msg["title"]] = msg messages[msg["title"]] = msg
}) })
const userInfo = await restClient.fetchMe(); const userInfo = await client.user;
avatarUrl = `https://cdn.discordapp.com/avatars/${userInfo.id}/${userInfo.avatar}.png`;
const avatar = userInfo?.avatarURL() ?? '';
avatarUrl = avatar;
console.log(avatarUrl); console.log(avatarUrl);
} }
@ -32,14 +34,12 @@ export const getList = () => Object.keys(messages).map(msg => ({ name: messages[
export const getEmbed = (name: string) => { export const getEmbed = (name: string) => {
let foundMsg: Message | undefined; let foundMsg: Message | undefined;
Object.keys(messages).forEach((msgKey) => Object.keys(messages).forEach((msgKey) => {
{
if (foundMsg) return; if (foundMsg) return;
const msg = messages[msgKey] const msg = messages[msgKey]
if (msg.names.includes(name)) if (msg.names.includes(name)) {
{
foundMsg = msg; foundMsg = msg;
return; return;
} }

1140
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -5,17 +5,18 @@
"main": "bot.js", "main": "bot.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node bot.ts" "start": "ts-node bot.ts",
"start:watch": "ts-node-dev --respawn --cache-directory ./tmp -- ./bot.ts"
}, },
"author": "Lily Wonhalf <lilywonhalf@gmail.com>", "author": "Lily Wonhalf <lilywonhalf@gmail.com>",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.1.3", "axios": "^1.1.3",
"detritus-client-rest": "git+https://github.com/spiralw/DiscordRest#fix-build",
"detritus-client-socket": "git+https://github.com/spiralw/DiscordSocket#fix-build",
"discord-api-types": "^0.37.14", "discord-api-types": "^0.37.14",
"discord.js": "^14.14.1",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.4" "typescript": "^4.8.4"
} }
} }