我想让我的机器人能够通过命令触发来制作Webhook,然后Webhook可以说出一定间隔的消息。 我要获取已创建的Webhook的令牌和ID,然后将其放在setInterval上,该setInterval会一直执行直到删除该Webhook。
const Discord = require('discord.js');
const commando = require('discord.js-commando');
class pingy extends commando.Command
{
constructor(client) {
super(client, {
name: 'pinghook',
group: 'help',
memberName: 'pinghook',
description: 'git',
})
}
async run(message, args){
var args = Array.prototype.slice.call(arguments);
const nameAvatar = args.join(" ");
const linkCheck = /https?:\/\/.+\.(?:png|jpg|jpeg)/gi;
if (!linkCheck.test(nameAvatar)) return message.reply("You must supply an image link.");
const avatar = nameAvatar.match(linkCheck)[0];
const name = nameAvatar.replace(linkCheck, "");
const name2 = "PingBot";
message.channel.createWebhook(name, avatar)
.then(webhook => webhook.edit(name2, avatar)
.catch(error => console.log(error)))
.then(wb => hook
.catch(error => console.log(error)))
.catch(error => console.log(error));
const hook = new Discord.WebhookClient(wb.id, wb.token)
setInterval(() => {
hook.send("@everyone you've been pinged.")
}, 1500);
}};
module.exports = pingy;
这是我尝试编写此代码时遇到的一些错误。 ReferenceError: wb is not defined
我的期望:我的机器人可以创建由用户激活的Webhook,然后该Webhook会间隔一段时间发送一条消息,直到被删除。从最近创建的webhook中获取webhook.id和webhook.token即可。
现实: ReferenceError: wb is not defined
答案 0 :(得分:1)
代替
message.channel.createWebhook(name, avatar)
.then(webhook => webhook.edit(name2, avatar)
.catch(error => console.log(error)))
.then(wb => hook
.catch(error => console.log(error)))
.catch(error => console.log(error));
const hook = new Discord.WebhookClient(wb.id, wb.token)
setInterval(() => {
hook.send("@everyone you've been pinged.")
}, 1500);
通过使用async / await,您可以使其变得更加整洁和轻松:
const hook = await message.channel.createWebhook(name, avatar).catch(error => console.log(error))
await hook.edit(name2, avatar).catch(error => console.log(error))
setInterval(() => {
hook.send("@everyone you've been pinged.")
}, 1500);