|
| 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 | +} |
0 commit comments