Skip to content

Commit

Permalink
feat: split commands
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Oct 13, 2023
1 parent d95d250 commit 5d247c9
Showing 1 changed file with 172 additions and 56 deletions.
228 changes: 172 additions & 56 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import mysql from 'mysql2/promise'
import fetch from 'node-fetch'
import { LoggerFactory } from 'logger.js'
import { InteractionType, verifyKeyMiddleware } from 'discord-interactions'
import { RowDataPacket } from 'mysql2'

dotenv.config()

Expand Down Expand Up @@ -108,12 +109,15 @@ const clientId = Buffer.from(
async (req, res) => {
const interaction = req.body
if (
interaction.data.name === 'gate' &&
(interaction.data.name === 'gate-add' ||
interaction.data.name === 'gate-update' ||
interaction.data.name === 'gate-remove' ||
interaction.data.name === 'gate-clear') &&
interaction.type === InteractionType.APPLICATION_COMMAND
) {
const subCommand = interaction.data.options[0]
const user = subCommand.options.find((e: any) => e.name === 'user')
.value as string
const user = interaction.data.options.find(
(e: any) => e.name === 'user'
).value as string
if (!/^\d+$/.test(user)) {
return res.send({
type: 4,
Expand All @@ -122,8 +126,8 @@ const clientId = Buffer.from(
},
})
}
if (subCommand.name === 'update') {
const allowedServers = subCommand.options.find(
if (interaction.data.name === 'gate-update') {
const allowedServers = interaction.data.options.find(
(e: any) => e.name === 'allowed-servers'
).value as string
if (!/^\d+(?:,\d+)*$/.test(allowedServers)) {
Expand All @@ -148,7 +152,91 @@ const clientId = Buffer.from(
},
})
}
if (subCommand.name === 'remove') {
if (interaction.data.name === 'gate-add') {
if (!interaction.guild_id) {
return res.send({
type: 4,
data: {
content: 'ここでは使用できません。',
},
})
}
// fetch current servers
const result = (
await pool.query(
'SELECT `allowed_servers` FROM `allowed_users` WHERE `id` = ? LIMIT 1',
user
)
)[0] as RowDataPacket[]
const allowedServers =
result.length > 0
? (result[0]['allowed_servers'] as string).split(',')
: []
// add server
allowedServers.push(interaction.guild_id)
allowedServers.filter((e, i, a) => a.indexOf(e) === i)
logger.info(
`Added ${interaction.guild_id}, new allowed servers: ${allowedServers}`
)
// modify database
await pool.query(
'INSERT INTO `allowed_users` (`id`, `allowed_servers`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `allowed_servers` = VALUES(`allowed_servers`)',
[user, allowedServers.join(',')]
)
const url = `https://discord.com/oauth2/authorize?client_id=${clientId}&redirect_uri=${encodeURIComponent(
process.env.REDIRECT_URI || ''
)}&response_type=code&scope=guilds.join%20identify`
return res.send({
type: 4,
data: {
content: `ユーザー\`${user}\`を追加しました。下のURLをコピーして送信してください。\n${url}`,
},
})
}
if (interaction.data.name === 'gate-remove') {
if (!interaction.guild_id) {
return res.send({
type: 4,
data: {
content: 'ここでは使用できません。',
},
})
}
// fetch current servers
const result = (
await pool.query(
'SELECT `allowed_servers` FROM `allowed_users` WHERE `id` = ? LIMIT 1',
user
)
)[0] as RowDataPacket[]
let allowedServers =
result.length > 0
? (result[0]['allowed_servers'] as string).split(',')
: []
// remove server
allowedServers = allowedServers.filter(
(e) => e !== interaction.guild_id
)
logger.info(
`Removed ${interaction.guild_id}, new allowed servers: ${allowedServers}`
)
// modify database
if (allowedServers.length === 0) {
await pool.query('DELETE FROM `allowed_users` WHERE `id` = ?', user)
} else {
await pool.query(
'INSERT INTO `allowed_users` (`id`, `allowed_servers`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `allowed_servers` = VALUES(`allowed_servers`)',
[user, allowedServers.join(',')]
)
}
return res.send({
type: 4,
data: {
content: `ユーザー\`${user}\`をこのサーバーに参加できなくしました。`,
},
})
}
if (interaction.data.name === 'gate-clear') {
await pool.query('DELETE FROM `allowed_users` WHERE `id` = ?', user)
return res.send({
type: 4,
Expand All @@ -162,56 +250,84 @@ const clientId = Buffer.from(
}
)

await fetch(`https://discord.com/api/v10/applications/${clientId}/commands`, {
method: 'POST',
headers: {
'Authorization': `Bot ${process.env.BOT_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: 1, // slash command
name: 'gate',
description: 'Manages the "gate"',
options: [
{
type: 1, // subcommand
name: 'update',
description: 'Add or update an user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
{
type: 3, // string
name: 'allowed-servers',
description:
'Comma separated list of servers that the user can join',
required: true,
},
],
},
{
type: 1, // subcommand
name: 'remove',
description: 'Remove an user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
],
const pushCommand = async (command: any) => {
await fetch(
`https://discord.com/api/v10/applications/${clientId}/commands`,
{
method: 'POST',
headers: {
'Authorization': `Bot ${process.env.BOT_TOKEN}`,
'Content-Type': 'application/json',
},
],
}),
}).then(async (res) => {
logger.info('POST commands result: ' + (await res.text()))
body: JSON.stringify(command),
}
).then(async (res) => {
logger.info('POST commands result: ' + (await res.text()))
})
}

await pushCommand({
type: 1, // slash command
name: 'gate-add',
description: 'Add this server to allowed server list of an user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
],
})

await pushCommand({
type: 1,
name: 'gate-update',
description: 'Add or update an user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
{
type: 3, // string
name: 'allowed-servers',
description: 'Comma separated list of servers that the user can join',
required: true,
},
],
})

await pushCommand({
type: 1, // subcommand
name: 'gate-remove',
description: 'Remove current guild from allowed server list of an user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
],
})

await pushCommand({
type: 1, // subcommand
name: 'gate-clear',
description: 'Remove an ENTIRE user',
options: [
{
type: 3, // string
name: 'user',
description: 'User ID',
required: true,
},
],
})

app.listen(8080)
logger.info('Listening on 8080')
app.listen(process.env.PORT || 8080)
logger.info('Listening on ' + (process.env.PORT || 8080))
})()

0 comments on commit 5d247c9

Please sign in to comment.