【问题标题】:How to make a Webhook speak (loop) through a Discord Bot如何通过 Discord Bot 让 Webhook 说话(循环)
【发布时间】:2019-08-07 13:48:03
【问题描述】:

我想让我的机器人能够创建一个由命令触发的 webhook,然后 webhook 可以说出一段间隔的消息。 我想获取已创建的 webhook 的令牌和 id,然后将其放在一个持续不断的 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

【问题讨论】:

    标签: javascript node.js webhooks discord.js


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2020-11-11
      • 2017-12-07
      • 2021-06-07
      • 1970-01-01
      • 2020-03-12
      • 2023-03-21
      • 2020-08-17
      • 2019-03-05
      • 2020-11-02
      相关资源
      最近更新 更多