【问题标题】:Discord.js Meme Generator code not workingDiscord.js Meme Generator 代码不起作用
【发布时间】:2020-12-26 07:18:01
【问题描述】:

谁能告诉我为什么我的 Meme Generator 代码不起作用? 所以我在 discord.js 中制作了一个 meme 生成器,当有人说“gg!meme”时会生成随机 meme

client.on('message', message => {
    module.exports = {
        name: "meme",
        category: "fun",
        description: "Sends an epic meme",
        run: async (client, message, args) => {
            // In this array, 
            // you can put the subreddits you want to grab memes from
            const subReddits = ["dankmeme", "meme", "me_irl"];
            // Grab a random property from the array
            const random = subReddits[Math.floor(Math.random() * subReddits.length)];
    if (message.content === 'gg!meme') {
                // Get a random image from the subreddit page
                const img = await randomPuppy(random);
                const embed = new RichEmbed()
                    .setColor("RANDOM")
                    .setImage(img)
                    .setTitle(`From /r/${random}`)
                    .setURL(`https://reddit.com/r/${random}`);

                message.channel.send(embed);
            }
        }
    };
});

【问题讨论】:

  • 有什么问题?

标签: discord.js


【解决方案1】:

概述:是的,我知道为什么。看起来您已经完全复制并粘贴了一个代码源并将其放入您的代码中,而不了解它的作用。

问题:您在非命令处理程序脚本中输入了命令处理程序结构化命令。

问题:#2此代码已过时,似乎是 Discord.js v11

假设您的脚本在没有命令处理程序的情况下运行,这应该可以工作:

client.on('message', message => {
    if (message.content === 'gg!meme') {
         // In this array, 
            // you can put the subreddits you want to grab memes from
            const subReddits = ["dankmeme", "meme", "me_irl"];
            // Grab a random property from the array
            const random = subReddits[Math.floor(Math.random() * subReddits.length)];
                // Get a random image from the subreddit page
                const img = await randomPuppy(random);
                const embed = new Discord.MessageEmbed()
                    .setColor("RANDOM")
                    .setImage(img)
                    .setTitle(`From /r/${random}`)
                    .setURL(`https://reddit.com/r/${random}`);

                message.channel.send(embed);
            }
});

建议:

与其每次发出命令时都输入gg!,不如把它变成一个你可以随时引用的变量。

Example:
const prefix = 'gg!'

Usage:
if (message.content.startsWith(prefix + 'ping')) {
 return message.channel.send('pong!')
}

假设您还安装了 npm 包 randompuppy,这应该可以工作。

【讨论】:

  • 在 const img = await randomPuppy(random); 处有问题“等待”有问题
  • 去掉await函数,没注意很抱歉。
【解决方案2】:

您使用的是过时版本的 discord.js。

通过在终端中输入 npm i discord.js@latest 来安装最新版本的 discord.js。在最新版本中,RichEmbed 被替换为 MessageEmbed

【讨论】:

  • 老实说,我删除了该代码,但它仍然有效,所以谢谢
猜你喜欢
  • 2020-12-11
  • 2021-07-27
  • 2022-01-12
  • 2020-10-11
  • 2012-03-15
  • 2018-04-24
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多