英文:
How would I write a command to rename the current channel the command is used in with DiscordJS v13?
问题
我正在尝试创建一个命令,可以通过 /rename
来重命名使用该命令的当前频道。在 discord.js 文档中,它说只需编写以下内容:
channel
.setName('not_general')
.then((newChannel) => console.log(`Channel's new name is ${newChannel.name}`))
.catch(console.error);
但是在测试时,它显示交互失败。有人知道如何处理吗?
module.exports = {
name: 'rename',
description: 'Renames current channel',
permission: 'ADMINISTRATOR',
/**
*
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
channel
.setName('not_general')
.then((newChannel) =>
console.log(`Channel's new name is ${newChannel.name}`),
)
.catch(console.error);
changeEmbed = new MessageEmbed()
.setColor('#5665da')
.setTitle('Ticket Update')
.setDescription(`Ticket Channel Name Set To ${newChannel}`)
.setTimestamp();
interaction.reply({ embeds: [changeEmbed], ephemeral: true });
},
};
注意:在代码中,有一些错误,可能需要进一步修复。
英文:
I'm trying to create a command that will rename the current channel the command is used in via /rename
. On the discord.js docs, it says to just write:
channel
.setName('not_general')
.then((newChannel) => console.log(`Channel's new name is ${newChannel.name}`))
.catch(console.error);
But when testing it says interaction failed. Does anyone know how to go about this?
module.exports = {
name: 'rename',
description: 'Renames current channel',
permission: 'ADMINISTRATOR',
/**
*
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
channel
.setName('not_general')
.then((newChannel) =>
console.log(`Channel's new name is ${newChannel.name}`),
)
.catch(console.error);
changeEmbed = new MessageEmbed()
.setColor('#5665da')
.setTitle('Ticket Update')
.setDescription(`Ticket Channel Name Set To ${newChannel}`)
.setTimestamp();
interaction.reply({ embeds: [changeEmbed], ephemeral: true });
},
};
答案1
得分: 2
我认为你的控制台也会收到错误消息,因为你不能在then()
之外使用newChannel
变量。你已经在使用async
,你可以使用await
来等待机器人更改频道的名称。
此外,没有channel
变量。你是不是指的是interaction.channel
?那是交互消息发送的频道。
module.exports = {
name: 'rename',
description: '重命名当前频道',
permission: 'ADMINISTRATOR',
/**
*
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
try {
let newChannel = await interaction.channel.setName('not_general');
console.log(`频道的新名称是 ${newChannel.name}`);
let changeEmbed = new MessageEmbed()
.setColor('#5665da')
.setTitle('票务更新')
.setDescription(`票务频道名称已设置为 ${newChannel}`)
.setTimestamp();
interaction.reply({ embeds: [changeEmbed], ephemeral: true });
} catch (error) {
console.error(error);
}
},
};
英文:
I think you also receive an error message on your console because you can't use the newChannel
variable outside your then()
. You're already using async
you could use await
to wait for the bot to change the channel's name.
Also, there is no channel
variable. Did you mean interaction.channel
? That's the channel the interaction was sent in.
module.exports = {
name: 'rename',
description: 'Renames current channel',
permission: 'ADMINISTRATOR',
/**
*
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
try {
let newChannel = await interaction.channel.setName('not_general');
console.log(`Channel's new name is ${newChannel.name}`);
let changeEmbed = new MessageEmbed()
.setColor('#5665da')
.setTitle('Ticket Update')
.setDescription(`Ticket Channel Name Set To ${newChannel}`)
.setTimestamp();
interaction.reply({ embeds: [changeEmbed], ephemeral: true });
} catch (error) {
console.error(error);
}
},
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论