Skip to content

Commit 222d24c

Browse files
committed
wip
1 parent 1d37c36 commit 222d24c

File tree

5 files changed

+98
-9
lines changed

5 files changed

+98
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"author": "Michał Miszczyszyn <[email protected]> (https://typeofweb.com/)",
2424
"license": "AGPL",
2525
"dependencies": {
26+
"@discordjs/rest": "1.1.0",
2627
"@types/bluebird": "3.5.36",
2728
"algoliasearch": "4.14.2",
2829
"bluebird": "3.7.2",

src/app.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import Discord, { GatewayIntentBits } from 'discord.js';
1+
import Discord, { GatewayIntentBits, REST, Routes, SlashCommandBuilder } from 'discord.js';
22
import MonitoRSS from 'monitorss';
33
import type { ClientConfig } from 'monitorss';
44
import Cache from 'node-cache';
55

6-
import { handleCommand } from './commands';
6+
import { handleCommand, handleSlashCommand, legacyCommandsToSlashCommands } from './commands';
77
import { KARMA_REGEX } from './commands/karma';
88
import { messageToReflinks } from './commands/reflink';
99
import { getConfig } from './config';
@@ -81,6 +81,16 @@ function isCommand(msg: Discord.Message) {
8181
return msg.content.startsWith(getConfig('PREFIX')) || KARMA_REGEX.test(msg.content);
8282
}
8383

84+
client.on('interactionCreate', async (interaction) => {
85+
if (!interaction.isChatInputCommand()) return;
86+
87+
try {
88+
await handleSlashCommand(interaction);
89+
} catch (err) {
90+
console.error(err);
91+
}
92+
});
93+
8494
// const ROLE_MUTED_NAME = 'muted' as const;
8595
// const MAX_MENTIONS_PER_MESSAGE = 10;
8696

@@ -190,11 +200,25 @@ client.on('messageDelete', async (msg) => {
190200
});
191201

192202
async function init() {
203+
const rest = new REST({ version: '10' }).setToken(getConfig('DISCORD_BOT_TOKEN'));
204+
205+
const TYPE_OF_WEB_GUILD_ID = '440163731704643589';
206+
207+
try {
208+
await rest.put(
209+
Routes.applicationGuildCommands(getConfig('DISCORD_CLIENT_ID'), TYPE_OF_WEB_GUILD_ID),
210+
{
211+
body: legacyCommandsToSlashCommands(),
212+
},
213+
);
214+
} catch (err) {
215+
console.error(err);
216+
}
217+
193218
await client.login(getConfig('DISCORD_BOT_TOKEN'));
194219

195220
// Auto assign roles
196221
{
197-
const TYPE_OF_WEB_GUILD_ID = '440163731704643589';
198222
const guild = await client.guilds.fetch(TYPE_OF_WEB_GUILD_ID);
199223
void updateRoles(guild);
200224
setInterval(() => {

src/commands/index.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Discord from 'discord.js';
1+
import Discord, { SlashCommandBuilder } from 'discord.js';
22
import type { PermissionsString } from 'discord.js';
33

44
import { getConfig } from '../config';
@@ -143,26 +143,80 @@ function printHelp(msg: Discord.Message, member: Discord.GuildMember) {
143143
});
144144
}
145145

146+
export const legacyCommandsToSlashCommands = () => {
147+
return allCommands
148+
.filter((legacyCommand) => legacyCommand.name)
149+
.map((legacyCommand) => {
150+
const slashCommand = new SlashCommandBuilder()
151+
.setName(legacyCommand.name)
152+
.setDescription(legacyCommand.description.slice(0, 100));
153+
154+
if (legacyCommand.args === 'optional') {
155+
slashCommand.addStringOption((option) =>
156+
option.setName('arg').setDescription('arg').setRequired(false),
157+
);
158+
}
159+
if (legacyCommand.args === 'required') {
160+
slashCommand.addStringOption((option) =>
161+
option.setName('arg').setDescription('arg').setRequired(true),
162+
);
163+
}
164+
165+
return slashCommand;
166+
})
167+
.map((c) => c.toJSON());
168+
};
169+
170+
export const handleSlashCommand = async (
171+
interaction: Discord.ChatInputCommandInteraction<Discord.CacheType>,
172+
) => {
173+
if (!interaction.isChatInputCommand()) {
174+
return;
175+
}
176+
177+
try {
178+
const content = `${getConfig('PREFIX')}${interaction.commandName} ${
179+
interaction.options.getString('arg') ?? ''
180+
}`.trim();
181+
// @ts-ignore
182+
interaction.content = content;
183+
// @ts-ignore
184+
interaction.author = interaction.user;
185+
// @ts-ignore
186+
interaction.channel.send = interaction.reply.bind(interaction);
187+
// @ts-ignore
188+
await handleCommand(interaction);
189+
} catch (err) {
190+
console.error(err);
191+
}
192+
};
193+
146194
export function handleCommand(msg: Discord.Message) {
195+
console.log(1);
147196
if (!msg.guild) {
148197
return undefined;
149198
}
150199

200+
console.log(2);
151201
if (KARMA_REGEX.test(msg.content)) {
152202
return processCommand(msg, addKarma, null);
153203
}
204+
console.log(3);
154205

155206
const [, maybeCommand, rest] = COMMAND_PATTERN.exec(msg.content) || [null, null, null];
156207

208+
console.log(4, msg.content);
157209
if (maybeCommand === 'help') {
158210
const member = msg.guild.members.cache.get(msg.author.id);
159211
if (member) {
160212
return printHelp(msg, member);
161213
}
162214
}
163215

216+
console.log(5);
164217
const command = allCommands.find((c) => maybeCommand === c.name);
165218

219+
console.log(6, command, maybeCommand);
166220
if (!command || !maybeCommand) {
167221
return undefined;
168222
}
@@ -171,30 +225,40 @@ export function handleCommand(msg: Discord.Message) {
171225
}
172226

173227
async function processCommand(msg: Discord.Message, command: Command, rest: string | null) {
228+
console.log(10);
229+
console.log(msg.guild);
174230
const member = msg.guild?.members.cache.get(msg.author.id);
175231

232+
console.log(11);
176233
if (!member || (command.permissions && !member.permissions.has(command.permissions))) {
177234
return undefined; // silence is golden
178235
}
179236

237+
console.log(12);
180238
await msg.channel.sendTyping();
181239

240+
console.log(13);
182241
if (command.guildOnly && msg.channel.type !== Discord.ChannelType.GuildText) {
183242
throw new InvalidUsageError(`to polecenie można wywołać tylko na kanałach.`);
184243
}
185244

245+
console.log(14);
186246
verifyCooldown(msg, command);
187247
const args = rest ? rest.split(/\s+/g) : [];
188248

249+
console.log(15);
189250
if (command.args === 'optional') {
190251
return command.execute(msg, args);
191252
}
253+
console.log(16);
192254
if (!args.length && command.args === 'required') {
193255
throw new InvalidUsageError(`nie podano argumentów!`);
194256
}
257+
console.log(17);
195258
if (args.length && command.args === 'prohibited') {
196259
throw new InvalidUsageError(`argumenty niedozwolone!`);
197260
}
198261

262+
console.log(18);
199263
return command.execute(msg, args);
200264
}

tsconfig.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
"emitDecoratorMetadata": true,
66
"esModuleInterop": true,
77
"experimentalDecorators": true,
8-
"lib": ["es2021"],
8+
"lib": ["es2022"],
99
"module": "commonjs",
1010
"noFallthroughCasesInSwitch": true,
1111
"noImplicitReturns": true,
12-
"noUnusedLocals": true,
13-
"noUnusedParameters": true,
12+
"noUnusedLocals": false,
13+
"noUnusedParameters": false,
1414
"outDir": "dist",
1515
"resolveJsonModule": true,
1616
"rootDir": ".",
1717
"skipLibCheck": true,
1818
"sourceMap": true,
1919
"strict": true,
20-
"target": "es2019",
20+
"target": "es2022",
2121
"typeRoots": ["./node_modules/@types", "./typings"]
2222
},
2323
"exclude": ["dist"]

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
combined-stream "^1.0.8"
342342
mime-types "^2.1.12"
343343

344-
"@discordjs/rest@^1.1.0":
344+
"@discordjs/rest@1.1.0", "@discordjs/rest@^1.1.0":
345345
version "1.1.0"
346346
resolved "https://registry.yarnpkg.com/@discordjs/rest/-/rest-1.1.0.tgz#5c283571a22b911ca334316245487af40baffe8b"
347347
integrity sha512-yCrthRTQeUyNThQEpCk7bvQJlwQmz6kU0tf3dcWBv2WX3Bncl41x7Wc+v5b5OsIxfNYq38PvVtWircu9jtYZug==

0 commit comments

Comments
 (0)