我需要获取已创建通道的id,但我不知道如何操作(discord.js)

yrdbyhpb  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(355)

我的bot在接收到特定的message.content后创建了一个不协调的频道,我需要获取它创建的频道id才能在消息中显示它。我的代码:

client.on('message', message  =>{
    if(message.content === (prefix + 'report')) {
            message.guild.channels.create (`Репорт (${message.author.tag})`, {
                type: 'voice',
                permissionOverwrites: [
                {
                    id: '861645891848110100',
                    deny: 'VIEW_CHANNEL',
                },
                {
                    id: message.author.id,
                    allow: ['VIEW_CHANNEL', 'STREAM', 'SPEAK', 'CONNECT','USE_VAD'],
                    deny: ['PRIORITY_SPEAKER', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'CREATE_INSTANT_INVITE', 'MANAGE_ROLES', 'MANAGE_CHANNELS'],
                },
                {
                    id: moderRole,
                    allow: ['VIEW_CHANNEL', 'STREAM', 'SPEAK', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'MANAGE_ROLES', 'CREATE_INSTANT_INVITE'],
                    deny: ['MANAGE_CHANNELS'],
                },
            ],
                reason: `${message.author.tag} sended report`,
        })

        const msg1 = new Discord.MessageEmbed()
        .setTitle(`Обращение успешно отправленно!`)
        .setDescription(`<@${message.author.id}>, просим вас зайти в канал <#$> и ждать пока вас переместят для разбирательств.`)

        message.reply(msg1)
        message.delete()
    }
})

新变化
好的,我稍微修改了我的代码,现在看起来是这样的:

client.on('message', async message => {
    if(message.content === (prefix + 'report')) {
            const channel = await message.guild.channels.create(
            message.guild.channels.create (`Репорт (${message.author.tag})`, {
                type: 'voice',
                permissionOverwrites: [
                {
                    id: '861645891848110100',
                    deny: 'VIEW_CHANNEL',
                },
                {
                    id: message.author.id,
                    allow: ['VIEW_CHANNEL', 'STREAM', 'SPEAK', 'CONNECT','USE_VAD'],
                    deny: ['PRIORITY_SPEAKER', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'CREATE_INSTANT_INVITE', 'MANAGE_ROLES', 'MANAGE_CHANNELS'],
                },
                {
                    id: moderRole,
                    allow: ['VIEW_CHANNEL', 'STREAM', 'SPEAK', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'MANAGE_ROLES', 'CREATE_INSTANT_INVITE'],
                    deny: ['MANAGE_CHANNELS'],
                },
            ],
                reason: `${message.author.tag} sended report`,
        }))
        const msg1 = new Discord.MessageEmbed()
        .setTitle(`Обращение успешно отправленно!`)
        .setDescription(`<@${message.author.id}>, просим вас зайти в канал <#${channel.id}> и ждать пока вас переместят для разбирательств.`)

        message.reply(msg1)
        message.delete()
    }
})

但我在测试后发现以下错误:

(node:1344) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
name: Could not interpret "{}" as string.
    at RequestHandler.execute (a:\Wiki Cheats\discord bot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (a:\Wiki Cheats\discord bot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async GuildChannelManager.create (a:\Wiki Cheats\discord bot\node_modules\discord.js\src\managers\GuildChannelManager.js:112:18)
    at async Client.<anonymous> (a:\Wiki Cheats\discord bot\index.js:35:29)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1344) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
csbfibhn

csbfibhn1#

您需要将新创建的通道保存为一个变量,以便以后引用。

const channel = await message.guild.channels.create(...)

然后,你可以使用 channel.id 在您的邮件或其他邮件中。
p、 你必须改变 client.on('message', message => {...})client.on('message', async message => {...}) 为了使用 await ,否则您将不得不使用 .then() 使用嵌套回调。

相关问题