【发布时间】:2020-07-06 08:41:58
【问题描述】:
我正在编写一个不和谐的机器人,它有一些命令,由我制作的命令处理程序处理。我将在下面包含代码。我的问题是,如果我启用该命令,它会在机器人所在的每个服务器上切换。那么我如何实现一个message.guild.id 系统,它只切换服务器中输入命令的命令?
Christian.js - 从处理程序读取的外部文件
const Discord = require('discord.js')
exports.run = (bot, message, args, christian) => {
if (args[0] === 'off') christian = false
else if (args[0] === 'on') christian = true
message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
return christian[message.guild.id]
}
exports.help = {
name: 'christian'
}
- 我曾尝试添加公会,但惨遭失败 (部分)index.js - 主文件
bot.on('message', msg => {
const {author, channel, content} = msg
if (author.bot) return
try {
if (content.startsWith(prefix)) {
const args = content.toLowerCase().substring(prefix.length).split(/\s+/g)
const command = args.shift()
readdir(join(__dirname, 'commands')).then(files => {
files.map(file => require(join(__dirname, 'commands', file))).forEach(cmd => {
if (command === cmd.help.name) {
const resultchristian = cmd.run(bot, msg, args, christian)
if (typeof resultchristian !== 'undefined') christian = resultchristian
}
})
})
return
}
} catch (error) {
console.log('Command loading error has cccured')
}
if (christian[msg.guild.id]) {
const messages = {
fdck: 'frick',
biadtch: 'nasty person',
nadr: 'nibba',
ndga: 'smelly person',
padsy: 'child',
pude: 'child',
casdnt: 'threat to society',
bdrd: 'threat to society',
dsk: 'widly'
}
try {
const message = messages[content.toLowerCase()]
if (message) msg.delete() & channel.send(message).then((m) => m.delete(10000));
} catch (error) {
console.log('An error has occurred - make sure the bot has delete message permissions')
}
}
})
由于某种原因,我无法让我的代码与公会一起使用。
提前谢谢你,
乔(原型)
【问题讨论】:
标签: javascript node.js discord discord.js