generated from imranbarbhuiya/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add support for interactions * test: add more tests * feat: add option resolver * docs: fix links * test: use dapi-type * test: add some temporary tests * fix: add sub-command-group prefix * fix: mentionable test * test: add some temporary tests to make sure everything is working Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com>
- Loading branch information
1 parent
6ed4692
commit daea9db
Showing
18 changed files
with
400 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/tagscript-plugin-discord/src/lib/Transformer/Interaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CommandInteraction } from 'discord.js'; | ||
import { BaseTransformer } from './Base'; | ||
|
||
export class InteractionTransformer extends BaseTransformer<CommandInteraction> { | ||
protected override updateSafeValues() { | ||
this.safeValues.applicationId = this.base.applicationId; | ||
this.safeValues.channelId = this.base.channelId; | ||
this.safeValues.guildId = this.base.guildId; | ||
this.safeValues.commandId = this.base.commandId; | ||
this.safeValues.commandName = this.base.commandName; | ||
this.safeValues.locale = this.base.locale; | ||
this.safeValues.guildLocale = this.base.guildLocale; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/tagscript-plugin-discord/src/lib/Utils/CommandInteraction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Channel, CommandInteractionOption, CommandInteractionOptionResolver, GuildMember, Role, User } from 'discord.js'; | ||
import { IntegerTransformer, ITransformer, StringTransformer } from 'tagscript'; | ||
import { ChannelTransformer, MemberTransformer, RoleTransformer, UserTransformer } from '../Transformer'; | ||
|
||
export const mapOptions = (options: readonly CommandInteractionOption[], transformers: Record<string, ITransformer>, prefix = '') => { | ||
options.forEach((data) => { | ||
switch (data.type) { | ||
case 'SUB_COMMAND_GROUP': | ||
transformers.subCommandGroup = new StringTransformer(data.value as string); | ||
mapOptions(data.options!, transformers, `${data.name}-`); | ||
break; | ||
case 'SUB_COMMAND': | ||
transformers.subCommand = new StringTransformer(data.value as string); | ||
mapOptions(data.options!, transformers, `${prefix}${data.name}-`); | ||
break; | ||
case 'STRING': | ||
transformers[prefix + data.name] = new StringTransformer(data.value as string); | ||
break; | ||
case 'BOOLEAN': | ||
transformers[prefix + data.name] = new StringTransformer(data.value as string); | ||
break; | ||
case 'INTEGER': | ||
transformers[prefix + data.name] = new IntegerTransformer(data.value as `${number}`); | ||
break; | ||
case 'NUMBER': | ||
transformers[prefix + data.name] = new IntegerTransformer(data.value as `${number}`); | ||
break; | ||
case 'MENTIONABLE': | ||
transformers[prefix + data.name] = | ||
data.member instanceof GuildMember | ||
? new MemberTransformer(data.member) | ||
: data.role instanceof Role | ||
? new RoleTransformer(data.role) | ||
: data.user instanceof User | ||
? new UserTransformer(data.user) | ||
: // added only for test. Will be removed after rewriting these tests | ||
new StringTransformer(data.value as string); | ||
break; | ||
case 'USER': | ||
transformers[prefix + data.name] = | ||
data.member instanceof GuildMember | ||
? new MemberTransformer(data.member) | ||
: data.user | ||
? new UserTransformer(data.user) | ||
: // added only for test. Will be removed after rewriting these tests | ||
new StringTransformer(data.value as string); | ||
break; | ||
case 'ROLE': | ||
data.role instanceof Role && (transformers[prefix + data.name] = new RoleTransformer(data.role)); | ||
break; | ||
case 'CHANNEL': | ||
data.channel instanceof Channel && (transformers[prefix + data.name] = new ChannelTransformer(data.channel)); | ||
break; | ||
case 'ATTACHMENT': | ||
transformers[prefix + data.name] = new StringTransformer(data.attachment!.url); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* | ||
* Resolves {@link https://discord.js.org/#/docs/discord.js/stable/class/CommandInteractionOptionResolver CommandInteractionOptionResolver} options into {@link Record<string, ITransformer>}. | ||
* | ||
* @usage | ||
* ```typescript | ||
* client.on('interactionCreate', async interaction => { | ||
* if (!interaction.isCommand()) return; | ||
* | ||
* if (interaction.commandName === 'ping') { | ||
* const result = await ts.run(str, resolveCommandOptions(interaction.options)); | ||
* await interaction.reply(result.body); | ||
* } | ||
}); | ||
* ``` | ||
*/ | ||
export const resolveCommandOptions = (options: Omit<CommandInteractionOptionResolver, 'getMessage' | 'getFocused'>) => { | ||
const optionData = options.data; | ||
|
||
const transformers: Record<string, ITransformer> = {}; | ||
|
||
mapOptions(optionData, transformers); | ||
|
||
return transformers; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CommandInteraction'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './interfaces'; | ||
export * from './Transformer'; | ||
export * from './Parsers'; | ||
export * from './Utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.