message component interactions

This commit is contained in:
DjDeveloperr 2021-04-23 11:07:55 +05:30
parent 17e74dce45
commit b97ec3c225
18 changed files with 461 additions and 86 deletions

184
test/components.ts Normal file
View file

@ -0,0 +1,184 @@
import {
CommandClient,
Command,
CommandContext,
ButtonStyle,
MessageComponentType,
isMessageComponentInteraction,
MessageComponentInteraction,
Message
} from '../mod.ts'
import { TOKEN } from './config.ts'
const client = new CommandClient({
prefix: '.',
spacesAfterPrefix: true
})
enum Choice {
Rock,
Paper,
Scissor
}
const games = new Map<
string,
{ user: number; bot: number; msg: Message; txt: string }
>()
const components = [
{
type: MessageComponentType.ActionRow,
components: [
{
type: MessageComponentType.Button,
style: ButtonStyle.Primary,
label: 'Rock',
customID: 'rps::Rock'
},
{
type: MessageComponentType.Button,
style: ButtonStyle.Primary,
label: 'Paper',
customID: 'rps::Paper'
},
{
type: MessageComponentType.Button,
style: ButtonStyle.Primary,
label: 'Scissor',
customID: 'rps::Scissor'
}
]
}
]
client.once('ready', () => {
console.log('Ready!')
})
client.commands.add(
class extends Command {
name = 'button'
execute(ctx: CommandContext): void {
ctx.channel.send('Test Buttons', {
components: [
{
type: MessageComponentType.ActionRow,
components: [
{
type: MessageComponentType.Button,
label: 'Primary',
style: ButtonStyle.Primary,
customID: '1'
},
{
type: MessageComponentType.Button,
label: 'Secondary',
style: ButtonStyle.Secondary,
customID: '2'
},
{
type: MessageComponentType.Button,
label: 'Destructive',
style: ButtonStyle.Destructive,
customID: '3'
},
{
type: MessageComponentType.Button,
label: 'Success',
style: ButtonStyle.Success,
customID: '4'
},
{
type: MessageComponentType.Button,
label: 'Link',
style: ButtonStyle.Link,
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
}
]
}
]
})
}
}
)
client.commands.add(
class extends Command {
name = 'play'
execute(ctx: CommandContext): any {
if (games.has(ctx.author.id))
return ctx.message.reply('You are already playing!')
ctx.channel
.send('Game starts now!', {
components
})
.then((msg) => {
games.set(ctx.author.id, {
user: 0,
bot: 0,
msg,
txt: 'Game starts now!'
})
})
}
}
)
// client.on('raw', (e, d) => {
// if (e === 'INTERACTION_CREATE') console.log(e, d)
// })
client.on('interactionCreate', (i) => {
if (isMessageComponentInteraction(i) === true) {
const d = i as MessageComponentInteraction
if (d.customID.startsWith('rps::') === true) {
const game = games.get(d.user.id)
if (game === undefined) return
const choice = d.customID.split('::')[1]
const c: number = Number(Choice[choice as any])
const rand = Math.floor(Math.random() * 2)
game.txt += '\n\n'
game.txt += `You: ${choice}, Bot: ${Choice[rand]}`
let msg
if (rand === c) {
msg = 'Both chose ' + Choice[rand] + '!'
} else if (
(rand === 0 && c === 2) ||
(rand === 1 && c === 0) ||
(rand === 2 && c === 1)
) {
msg = 'Bot got one point!'
game.bot++
} else {
msg = 'You got one point!'
game.user++
}
game.txt += '\nInfo: ' + msg
if (game.bot === 5 || game.user === 5) {
const won = game.bot === 5 ? 'Bot' : 'You'
game.msg.edit(
`${won} won!\n\n**Points:** You: ${game.user} | Bot: ${game.bot}`,
{
components: []
}
)
games.delete(d.user.id)
} else {
game.msg.edit(
`${game.txt}\n\n**Points:** You: ${game.user} | Bot: ${game.bot}`,
{
components
}
)
}
}
}
})
console.log('Connecting...')
client.connect(TOKEN, ['GUILDS', 'GUILD_MESSAGES', 'DIRECT_MESSAGES'])

View file

@ -8,12 +8,12 @@ import {
CommandContext,
Extension,
Collection,
GuildTextChannel
} from '../../mod.ts'
GuildTextChannel,
slash,
SlashCommandInteraction
} from '../mod.ts'
import { LL_IP, LL_PASS, LL_PORT, TOKEN } from './config.ts'
import { Manager, Player } from 'https://deno.land/x/lavadeno/mod.ts'
import { Interaction } from '../structures/slash.ts'
import { slash } from '../client/mod.ts'
// import { SlashCommandOptionType } from '../types/slash.ts'
export const nodes = [
@ -58,12 +58,12 @@ class MyClient extends CommandClient {
}
@subslash('cmd', 'sub-cmd-no-grp')
subCmdNoGroup(d: Interaction): void {
subCmdNoGroup(d: SlashCommandInteraction): void {
d.respond({ content: 'sub-cmd-no-group worked' })
}
@groupslash('cmd', 'sub-cmd-group', 'sub-cmd')
subCmdGroup(d: Interaction): void {
subCmdGroup(d: SlashCommandInteraction): void {
d.respond({ content: 'sub-cmd-group worked' })
}
@ -79,7 +79,7 @@ class MyClient extends CommandClient {
}
@slash()
run(d: Interaction): void {
run(d: SlashCommandInteraction): void {
console.log(d.name)
}