【问题标题】:Trying to make a random command for my Discord Bot尝试为我的 Discord Bot 发出随机命令
【发布时间】:2021-01-31 23:49:16
【问题描述】:

每个命令都有单独的文件夹。我将它们连接到 main.js - 这是我的 GIF 文件夹。

module.exports = {
    name: 'gif',
    description: "this is a gif!",
    execute(message, args){
        
        if (message.content === "-gif") { // checks if the message says "?random"
                const number = Math.random(); // generates a random number
                message.channel.send(number.toString()); // sends a message to the channel with the number
        }
    }
}

现在它会生成随机数。我希望它生成随机 GIF(但我想通过链接添加它选择的所有随机 GIF)。

【问题讨论】:

  • 我会制作一个 gif 链接数组(我不确定你从哪里得到它们,所以我不知道你是否可以自动构建数组)并使用数字来引用和索引在那个数组中。拥有数组后,使用 [int](math.random() * myArray.length) 获取相关索引。
  • 我是从 tenor.com 获取的
  • 我是制作不和谐机器人的新手。你能给我看一个示例代码吗?

标签: discord discord.js


【解决方案1】:

定义不和谐

const Discord = require('discord.js');
const client = Discord.Client();

添加消息事件监听器

client.on('message', message => {
    //code goes here
});

从数组生成随机链接

const Discord = require('discord.js'); // defining the discord.js module
const client = Discord.Client(); //defining the bots client

client.on('message', message => { //a message event listener
    if (message.content.toLowerCase().startsWith('-gif') { // checking if the message is equal to "-gif"
        var gifs = ['linkToGif', 'linkToAnotherGif', 'etc'] //creating your array
        var response = Math.floor(Math.random * gifs.length); //getting a random number from the array
        message.channel.send(gifs[response] //sending the random link from the array
    }
});
client.login('yourToken');

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2018-04-17
    • 2021-01-06
    • 2020-05-11
    • 2018-11-21
    • 1970-01-01
    • 2021-06-05
    • 2021-11-14
    • 2021-11-07
    相关资源
    最近更新 更多