1 |
import { AutocompleteInteraction, CacheType, CommandInteraction, Message } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
import dot from 'dot-object'; |
8 |
import { fetchEmoji } from '../../utils/Emoji'; |
9 |
import AutoCompleteOptions from '../../types/AutoCompleteOptions'; |
10 |
|
11 |
export default class ConfigCommand extends BaseCommand { |
12 |
supportsInteractions: boolean = true; |
13 |
configDotted: { [key: string]: string[] } = {}; |
14 |
|
15 |
constructor() { |
16 |
super('config', 'settings', []); |
17 |
const config = DiscordClient.client.config.props; |
18 |
|
19 |
for (const guild in config) { |
20 |
this.configDotted[guild] = Object.keys(dot.dot({...config[guild]})); |
21 |
} |
22 |
} |
23 |
|
24 |
async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction<CacheType>, options: AutoCompleteOptions): Promise<void> { |
25 |
const focused = interaction.options.getFocused(true); |
26 |
|
27 |
if (focused.name !== "key") { |
28 |
return; |
29 |
} |
30 |
|
31 |
if (focused.value === '') { |
32 |
await interaction.respond(this.configDotted[interaction.guild!.id].slice(0, 25).map(key => ({ |
33 |
name: key, |
34 |
value: key, |
35 |
}))); |
36 |
|
37 |
return; |
38 |
} |
39 |
|
40 |
const response = []; |
41 |
|
42 |
for (const key of this.configDotted[interaction.guild!.id]) { |
43 |
if (key.includes(focused.value)) { |
44 |
response.push({ |
45 |
name: key, |
46 |
value: key |
47 |
}); |
48 |
|
49 |
if (response.length >= 25) { |
50 |
break; |
51 |
} |
52 |
} |
53 |
} |
54 |
|
55 |
await interaction.respond(response); |
56 |
} |
57 |
|
58 |
async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
59 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
60 |
await message.reply({ |
61 |
embeds: [ |
62 |
new MessageEmbed() |
63 |
.setColor('#f14a60') |
64 |
.setDescription(`This command requires at least one argument.`) |
65 |
] |
66 |
}); |
67 |
|
68 |
return; |
69 |
} |
70 |
|
71 |
const keyMap = options.isInteraction ? options.options.getString('key') : options.args[0]; |
72 |
const value = options.isInteraction ? options.options.getString('value') : options.args[1]; |
73 |
|
74 |
if (keyMap && !value) { |
75 |
const val = dot.pick(keyMap, client.config.props[message.guild!.id]); |
76 |
|
77 |
await message.reply({ |
78 |
content: val === undefined ? `${await fetchEmoji('error')} The given configuration key does not exist.` : (typeof val === 'object' ? "```json" + JSON.stringify(val, null, 2) + "```" : (val + '')) |
79 |
}); |
80 |
} |
81 |
else if (keyMap && value) { |
82 |
if (dot.pick(keyMap, client.config.props[message.guild!.id]) === undefined) { |
83 |
await message.reply({ content: `${await fetchEmoji('error')} The given configuration key does not exist.` }); |
84 |
return; |
85 |
} |
86 |
|
87 |
dot.set(keyMap, value, client.config.props[message.guild!.id]); |
88 |
await message.reply({ content: `${await fetchEmoji('check')} Configuration updated.` }); |
89 |
} |
90 |
} |
91 |
} |