gpt4 book ai didi

javascript - Discord.js - 如何在一台服务器中切换命令?

转载 作者:行者123 更新时间:2023-12-02 21:21:42 27 4
gpt4 key购买 nike

我正在构建一个带有命令处理程序和一些 meme 命令的不和谐机器人。我的问题是,当我激活“基督教模式”或“无咒骂模式”时,它会在机器人所在的每个服务器上激活。我的基督教模式代码在这里,跨 Christian.js 和 Index.js。

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 ? 'Active' : 'Inactive'}`)
return christian
}

exports.help = {
name: 'christian'
}

index.js(部分)

if (christian) {
const messages = {
flib: 'frick',
batch: 'nasty person',
nogger: 'nibba',
nafga: 'smelly person',
poohsy: 'child',
poohsyhole: 'child',
cant: 'threat to society',
bestard: 'threat to society',
dock: 'willy'
}
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')
}
}

提前致谢:)新代码:

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
}

这是我的前缀和命令处理程序,我需要更改什么才能使其正常工作吗?

最佳答案

您可以将 christian 设置为 object,然后添加 message.guild.id 作为具有任何值(true 或 false)的属性像这样

const Discord = require('discord.js')

exports.run = (bot, message, args, christian) => {
if (args[0] === 'off') christian[message.guild.id] = false
else if (args[0] === 'on') christian[message.guild.id] = true
message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
return christian
}

exports.help = {
name: 'christian'
}
let christian = {}

if (christian[message.guild.id]) {
const messages = {
flib: 'frick',
batch: 'nasty person',
nogger: 'nibba',
nafga: 'smelly person',
poohsy: 'child',
poohsyhole: 'child',
cant: 'threat to society',
bestard: 'threat to society',
dock: 'willy'
}
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 - Discord.js - 如何在一台服务器中切换命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60829715/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com