【问题标题】:How to create multiple discord bot statuses with Javascript如何使用 Javascript 创建多个不和谐机器人状态
【发布时间】:2019-03-28 22:16:12
【问题描述】:

我想添加一些东西,所以在它从 API 播放块之后,我希望它播放另一个状态。例如,如果它播放了几分钟,然后它会变成播放“hello world”之类的东西,然后在几分钟后又变回播放块。以下是我目前拥有的代码:

function updateBlock() {
    let response3 = axios.get(`https://google/api/getblockcount`)
    .then((response3) => {
        return Promise.resolve(response3);
    }).catch((error) => {
        console.log("Can not connect to API");
        console.log(error);
        return Promise.resolve({
            failed: true
        })
    });

    response3.then((response3)=> {
        if (response3.failed) {
            console.log("API Response Failed");
            return response3;
        }
        let b = response3.data;
        try {
            Client.user.setActivity("B: " + b, { type: 'WATCHING' })
            .then()
            .catch(console.error);
        } catch(err) {
            console.log("This is an API error catch");
            console.log(err);
        }
    });
}

Client.on("ready", () => {
    console.log(botName + " online!");
    updateBlock();
    setInterval(() => {
        updateBlock();
    }, 10000);
});

【问题讨论】:

    标签: javascript node.js bots discord.js


    【解决方案1】:

    假设您使用的是 discord.js 库,这是如何显示来自数组 textList 的随机项,其中也是从 API 接收的块高度,每 1 分钟更改一次。

    client.on('ready', () => {
        setInterval(async ()=>{
            let {data: blocks} = await axios.get('https://chain.ragnaproject.io/api/getblockcount').catch(console.log)
            let textList = ['Hello World','Lorem Ipsum','Discord Bots', 'Blocks: ' + blocks]
            var text = textList[Math.floor(Math.random() * textList.length)];
            client.user.setActivity(text , { type: 'WATCHING' })
        },60000) // milliseconds
    });
    

    【讨论】:

    • 谢谢,我会试试这个真的很感激
    • 无法启动,我也尝试过编辑 },60000); // 毫秒
    • 不可能,我使用自己的机器人测试了该代码。控制台有错误吗?
    • 你在client.on..client.user.setActivity...这两个地方都改了吗?
    • 感谢它运行良好,非常感谢您的帮助
    【解决方案2】:

    您可能想使用Client.user.setActivity():第一个参数是您要显示的游戏名称,第二个参数是options 对象。 options.type 定义游戏名称之前的字符串:“Playing”、“Watching”、“Listening to”和“Streaming”。 ActivityTypes分别是:

    'PLAYING'
    'WATCHING'
    'LISTENING'
    'STREAMING'
    

    如果您将'STREAMING'设置为类型,您还可以将options.url设置为您的直播网址。
    这是一个示例实现:

    client.user.setActivity("your sample text", {
      type: 'WATCHING'
    });
    // OR
    client.user.setActivity("Overwatch", {
      type: 'STREAMING',
      url: "https://example.com"
    });
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2021-11-10
      • 2020-03-19
      • 2020-01-10
      • 2018-03-20
      • 2020-09-06
      • 2021-04-10
      • 2022-10-24
      • 2020-12-28
      相关资源
      最近更新 更多