-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
157 lines (139 loc) · 4.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Client, GatewayIntentBits, Interaction, Options } from "discord.js";
import path from "path";
import GLOBALS from "./GLOBALS";
import GuildChatInteraction from "./src/classes/GuildChatInteraction";
import { preflight_checks } from "./src/handlers/Command";
import { CommandResponse } from "./src/handlers/CommandResponse";
import CrownBot from "./src/handlers/CrownBot";
import handle_autocomplete from "./src/handlers/handle_autocomplete";
import { handle_button } from "./src/handlers/handle_button";
import {
handle_editconfig,
handle_reportbug,
} from "./src/handlers/handle_modal";
import { config } from "dotenv";
config();
/*
# REQUIRED
======================================================================================================
DISCORD_CLIENTID: Bot's client ID
DISCORD_TOKEN: Discord API token
OWNER_ID: User ID of the bot owner
LASTFM_API_KEY: Last.fm API key
MONGO: Mongo DB connection string
======================================================================================================
LYRICS_ENDPOINT: Lyrics endpoint for the /lyrics command--command won't work unless this is set.
Setup a server to use it as:
<server>?gquery=<query string>
That <server> needs to be in the environment variable as LYRICS_ENDPOINT. /lyrics appends `?gquery=<query string>` to it.
Example, set it to https://mycoolsite.com/lyrics (only add trailing / when necessary) and it becomes https://mycoolsite.com/lyrics?gquery=something
SPOTIFY_CLIENTID: Spotify client ID for the /chart command to show artist images
SPOTIFY_SECRETID: Spotify client ID for the /chart command to show artist images
*/
(<any>global).appRoot = path.resolve(__dirname);
export type BotOptions = {
version: string;
buttons_version: string;
max_users: number;
client_id: string;
token: string;
owner_ID: string;
api_key: string;
mongo: string;
url: string;
wk_helper_endpoint: string;
};
(async () => {
try {
const {
DISCORD_CLIENTID,
DISCORD_TOKEN,
OWNER_ID,
LASTFM_API_KEY,
MONGO,
WK_HELPER_ENDPOINT,
} = process.env;
if (
!(
DISCORD_TOKEN &&
OWNER_ID &&
LASTFM_API_KEY &&
MONGO &&
DISCORD_CLIENTID &&
WK_HELPER_ENDPOINT
)
) {
throw "Some of the environment variables are missing.";
}
const bot = await new CrownBot({
version: GLOBALS.VERSION,
buttons_version: GLOBALS.BUTTONS_VERSION,
max_users: GLOBALS.MAX_USERS,
client_id: DISCORD_CLIENTID,
token: DISCORD_TOKEN,
owner_ID: OWNER_ID,
api_key: LASTFM_API_KEY,
mongo: MONGO,
url: GLOBALS.LASTFM_ENDPOINT,
wk_helper_endpoint: WK_HELPER_ENDPOINT,
}).init();
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences],
makeCache: Options.cacheWithLimits({
ReactionManager: 0,
GuildMemberManager: 0,
MessageManager: 0,
GuildEmojiManager: 0,
ThreadManager: 0,
ThreadMemberManager: 0,
}),
});
// register events
client.on("interactionCreate", async (interaction: Interaction) => {
if (interaction.isAutocomplete()) {
await handle_autocomplete(bot, client, interaction);
}
if (interaction.isButton()) {
await handle_button(bot, client, interaction);
return;
}
if (interaction.isModalSubmit()) {
if (interaction.customId === "bugmodal")
await handle_reportbug(bot, client, interaction);
else if (interaction.customId === "configmodal")
await handle_editconfig(bot, client, interaction);
return;
}
if (!interaction.isChatInputCommand()) return;
if (!interaction.guild) return;
const command = bot.commands.find(
(e) => e.data.name == interaction.commandName
);
if (!command) return;
try {
const response = new CommandResponse(bot, client, <any>interaction);
response.text;
const command_response = await preflight_checks(
bot,
client,
<GuildChatInteraction>interaction,
command,
response
);
if (
typeof command_response == "object" &&
command_response instanceof CommandResponse
) {
await command_response.reply();
}
} catch (e: any) {
console.error(e);
}
});
await client.login(DISCORD_TOKEN);
console.log(`Logged in as ${client.user?.tag}`);
} catch (e) {
console.log(e);
debugger; //eslint-disable-line
}
})();