Skip to content

Commit 7209aff

Browse files
committed
/lookup command
1 parent 6304f44 commit 7209aff

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

src/commands/api/lookup.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import {
2+
type ChatInputCommandInteraction,
3+
PermissionFlagsBits,
4+
SlashCommandBuilder,
5+
} from 'discord.js';
6+
7+
import type { Command } from '@/types';
8+
import { get } from '@/utils';
9+
10+
export class LookupCommand implements Command {
11+
public builder = new SlashCommandBuilder()
12+
.setName('lookup')
13+
.setDescription(
14+
'Verilen ckey, IP veya computer id ile eşleşen oyuncuları gösterir.'
15+
)
16+
.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels)
17+
.addSubcommand((subcommand) =>
18+
subcommand
19+
.setName('ckey')
20+
.setDescription('Verilen ckey ile eşleşen oyuncuları gösterir.')
21+
.addStringOption((option) =>
22+
option
23+
.setName('ckey')
24+
.setDescription('Oyuncunun ckeyi')
25+
.setRequired(true)
26+
.setAutocomplete(true)
27+
)
28+
)
29+
.addSubcommand((subcommand) =>
30+
subcommand
31+
.setName('ip')
32+
.setDescription('Verilen IP ile eşleşen oyuncuları gösterir.')
33+
.addStringOption((option) =>
34+
option
35+
.setName('ip')
36+
.setDescription('Oyuncunun IP adresi')
37+
.setRequired(true)
38+
)
39+
)
40+
.addSubcommand((subcommand) =>
41+
subcommand
42+
.setName('computerid')
43+
.setDescription('Verilen computer id ile eşleşen oyuncuları gösterir.')
44+
.addStringOption((option) =>
45+
option
46+
.setName('computerid')
47+
.setDescription("Oyuncunun computer id'si")
48+
.setRequired(true)
49+
)
50+
);
51+
public permissionRole = '1265385893733204138'; // this should be in config later
52+
public async execute(interaction: ChatInputCommandInteraction) {
53+
let rows: [string, string, string][] = [];
54+
55+
switch (interaction.options.getSubcommand()) {
56+
case 'ckey': {
57+
const ckey = interaction.options.getString('ckey', true);
58+
const { body } = await get<[string, string, string][]>(
59+
`player/lookup?ckey=${ckey}`
60+
);
61+
rows = body!;
62+
break;
63+
}
64+
case 'ip': {
65+
const ip = interaction.options.getString('ip', true);
66+
const { body } = await get<[string, string, string][]>(
67+
`player/lookup?ip=${ip}`
68+
);
69+
rows = body!;
70+
break;
71+
}
72+
case 'computerid': {
73+
const computerId = interaction.options.getString('computerid', true);
74+
const { body } = await get<[string, string, string][]>(
75+
`player/lookup?cid=${computerId}`
76+
);
77+
rows = body!;
78+
break;
79+
}
80+
}
81+
82+
const longestCkey = rows.reduce(
83+
(max, row) => Math.max(max, row[2].length),
84+
0
85+
);
86+
87+
// :tf:
88+
const message =
89+
'```md\n' +
90+
`| CID | IP | ${' '.repeat(Math.floor(longestCkey / 2) - 2)}CKEY${' '.repeat(Math.ceil(longestCkey / 2) - 2)} |\n` +
91+
`|------------|-----------------|-${'-'.repeat(longestCkey)}-|\n` +
92+
rows
93+
.map(
94+
([cid, ip, ckey]) =>
95+
`| ${cid} | ${ip.padEnd(15, ' ')} | ${ckey.padEnd(longestCkey, ' ')} |\n`
96+
)
97+
.join('') +
98+
'```';
99+
100+
await interaction.reply(message);
101+
}
102+
}

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './api/characters';
22
export * from './api/check';
3+
export * from './api/lookup';
34
export * from './api/player';
45
export * from './api/playtime';
56
export * from './api/verify';

src/events/interactionCreate/chatInput.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ export default async function chatInput(
1010
}
1111

1212
try {
13+
if (command.permissionRole) {
14+
const member = await interaction.guild?.members.fetch(
15+
interaction.user.id
16+
);
17+
18+
if (!member?.roles.cache.has(command.permissionRole)) {
19+
await interaction.reply({
20+
content: 'Bu komutu kullanmak için gerekli yetkiye sahip değilsin.',
21+
flags: MessageFlags.Ephemeral,
22+
});
23+
return;
24+
}
25+
}
26+
1327
await command.execute(interaction);
1428
} catch (error) {
1529
try {

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Command {
1313
| SlashCommandSubcommandsOnlyBuilder
1414
| SlashCommandOptionsOnlyBuilder
1515
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
16+
permissionRole?: string;
1617
execute: (interaction: ChatInputCommandInteraction) => Promise<void>;
1718
autocomplete?: (interaction: AutocompleteInteraction) => Promise<void>;
1819
}

0 commit comments

Comments
 (0)