【问题标题】:Discord.js Rule systemDiscord.js 规则系统
【发布时间】:2020-12-17 17:37:33
【问题描述】:

我开发了规则系统,但是有一个问题。当我输入 !rule 1 时,我想发送 rule 1 ,但错误提示:Cannot send an empty message

有代码:

const Discord = require('discord.js');
const fs = require('fs')
module.exports = {
    name: 'rules',
    guildOnly: true,
    async execute(message, args) {
        const rules = JSON.parse(fs.readFileSync("./rules.json", 'utf8'));
        message.channel.send(rules.args.slice(1).join(" "))
    }
}

json 文件:

{
    "rule1": "**Admins and Discord Mods have the final judgement. If asked to stop, stop.**",
    "rule2": "Be respectful, civil, and welcoming. Sometimes discussion can get heated, but you are responsible for your own behavior.",
    "rule3": "No insults, racism, sexism, homophobia, transphobia, and other kinds of discriminatory speech. We do not welcome these types of speech.",
    "rule4": "No NSFW content (including chat, avatars, and nicknames).",
    "rule5": "Do not join the server to promote your content.\nDo not advertise streams, YouTube channels, Discord servers, Reddit posts, and referral links.",
    "rule6": "Unsolicited advertisements sent to members via private messages will result in instant removal from the server.",
    "rule7": "Do not solicit, buy, sell, trade, give away, or beg for accounts, cheats, **boosting**, RP, codes, money, referrals, or other goods.",
    "rule8": "This is not a dating server: e-dating, fawning, excessive flirting, role-playing, and other similar behavior are not allowed.",
    "rule9": ""
}

【问题讨论】:

标签: javascript discord.js


【解决方案1】:

args 不是rules 对象的属性。相反,您可以使用Object.entries() function

// const rules = JSON.parse(fs.readFileSync("./rules.json", 'utf8'));

const rules = {
  "rule1": "**Admins and Discord Mods have the final judgement. If asked to stop, stop.**",
  "rule2": "Be respectful, civil, and welcoming. Sometimes discussion can get heated, but you are responsible for your own behavior.",
  "rule3": "No insults, racism, sexism, homophobia, transphobia, and other kinds of discriminatory speech. We do not welcome these types of speech.",
  "rule4": "No NSFW content (including chat, avatars, and nicknames).",
  "rule5": "Do not join the server to promote your content.\nDo not advertise streams, YouTube channels, Discord servers, Reddit posts, and referral links.",
  "rule6": "Unsolicited advertisements sent to members via private messages will result in instant removal from the server.",
  "rule7": "Do not solicit, buy, sell, trade, give away, or beg for accounts, cheats, **boosting**, RP, codes, money, referrals, or other goods.",
  "rule8": "This is not a dating server: e-dating, fawning, excessive flirting, role-playing, and other similar behavior are not allowed.",
  "rule9": ""
};


// you can use this function to get all the rules, separated by 2 line breaks
console.log(Object.entries(rules).map(rule => rule.join(': ')).join('\n\n'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2021-05-24
    • 2020-11-06
    • 2019-10-13
    • 1970-01-01
    • 2023-03-03
    • 2019-02-26
    相关资源
    最近更新 更多