Discord.js 规则系统

Posted

技术标签:

【中文标题】Discord.js 规则系统【英文标题】:Discord.js Rule system 【发布时间】: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": ""

【问题讨论】:

args 不是规则对象的属性。 知道如何解决 这能回答你的问题吗? Dynamically access object property using variable 【参考方案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'))

【讨论】:

以上是关于Discord.js 规则系统的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js 票务系统不发送临时消息

Discord Bot Maker - MySQL 调平系统或 Discord.js - MySQL 调平系统

Discord.js 计数系统

Discord.js票务系统错误发送未定义错误

有没有办法使用 quick.db 制作 discord.js 日志系统

使用 Discord.js 的批准/拒绝系统