Discord.js:创建一个频道并在创建时通过网络钩子发送一些东西



我正在尝试使用discord.js创建一个简单的discordbot,当创建了一个具有特定名称的通道时,会在该通道中创建一个webhook并发送一些东西。我尝试了不同的代码,但没有结果(一段时间前我的代码有效,现在我不知道为什么它不起作用(。这是我正在使用的代码(并且在一段时间前工作过(:

bot.on('channelCreate', channel => {
try {
if (channel.name === 'test') {
var webhook = channel.createWebhook('test webhook')
webhook.send('Hello World')
}} catch (err) {
console.log(err)
}
})

我的错误是:

TypeError: webhook.send is not a function

有人能帮我吗?

.createWebhook返回promise,因此您必须使用.then或使用异步等待,请尝试以下操作:

bot.on('channelCreate', async (channel) => {
try {
if (channel.name === 'test') {
var webhook = await channel.createWebhook('test webhook')
webhook.send('Hello World')
}} catch (err) {
console.log(err)
}
})

最新更新