DiscordJS MongoDB 自定义前缀
DiscordJS MongoDB Custom Prefix
我正在尝试使用 MongoDB 创建自定义前缀命令,但是当我 运行 使用默认前缀或公会集前缀的任何命令时,没有任何反应,也没有错误
机器人代码
messageCreate.js 事件:
module.exports = async (client, message) => {
const serverSchema = require('../../models/prefixSchema')
let default_prefix = '.'
client.prefix = async function (message) {
let px;
const data = await serverSchema.findOne({ serverId: message.guild.id })
.catch(err => console.log(err))
if (data) {
px = data.prefix
} else {
px = default_prefix;
}
if (message.author.bot) return;
if (!message.content.toLowerCase().startsWith(px)) return;
if (!message.member) message.member = await message.guild.members.fetch(message.member.id);
if (!message.guild) return;
const args = message.content.slice(px.length).trim().split(/ +/g);
const cmd = args.shift().toLowerCase();
if (cmd.length === 0) return;
const command = client.commands.get(cmd) || client.commands.find((x) => x.aliases && x.aliases.includes(cmd));
if (command) {
if (command.timeout) {
if (Timeout.has(`${message.author.id}${command.name}`)) {
const embed = new MessageEmbed()
.setTitle('You are in timeout!')
.setColor(config.colour)
.setDescription(`:x: You need to wait **${humanizeDuration(command.timeout, { round: true })}** to use command again`)
.setColor('#ff0000')
return message.channel.send({ embeds: [embed] })
} else {
command.run(client, message, args);
Timeout.add(`${message.author.id}${command.name}`)
setTimeout(() => {
Timeout.delete(`${message.author.id}${command.name}`)
}, command.timeout);
}
} else {
command.run(client, message, args)
}
}
}
}
prefixSchema.js:
const mongoose = require('mongoose');
const serverSchema = new mongoose.Schema({
serverId: String,
prefix: String,
});
module.exports = mongoose.model('server', serverSchema);
我正在使用 Web 仪表板而不是自定义前缀命令来更改前缀,下面是将前缀保存到数据库的仪表板代码
仪表板代码
manager.js:
const Server = require('./serverModel');
module.exports = class Manager {
static async createServer(id) {
const result = new Server({
serverId: id,
prefix: null,
});
await result.save();
return result;
}
static async findServer(id) {
const result = await Server.findOne({ serverId: id });
return result;
}
static async getPrefix(id) {
const result = await Server.findOne({ serverId: id });
return result.prefix;
}
static async updateServerPrefix(id, prefix) {
const result = await Server.findOne({ serverId: id });
if (result) {
return await result.updateOne({ prefix });
}
else {
return;
}
}
};
serverModel.js:
const mongoose = require('mongoose');
const serverSchema = new mongoose.Schema({
serverId: String,
prefix: String,
});
module.exports = mongoose.model('server', serverSchema);
MongoDB Screenshot
您需要先从服务器初始化前缀。
const server = await db.servers.fetch({_id: serverId})
const prefix = server.prefix
您需要定义前缀并初始化它。
let prefix;
let data = await db.findOne({
guildID: message.guild.id
})
if(data === null) {
prefix = "."
} else {
prefix = data.Prefix
}
我正在尝试使用 MongoDB 创建自定义前缀命令,但是当我 运行 使用默认前缀或公会集前缀的任何命令时,没有任何反应,也没有错误
机器人代码
messageCreate.js 事件:
module.exports = async (client, message) => {
const serverSchema = require('../../models/prefixSchema')
let default_prefix = '.'
client.prefix = async function (message) {
let px;
const data = await serverSchema.findOne({ serverId: message.guild.id })
.catch(err => console.log(err))
if (data) {
px = data.prefix
} else {
px = default_prefix;
}
if (message.author.bot) return;
if (!message.content.toLowerCase().startsWith(px)) return;
if (!message.member) message.member = await message.guild.members.fetch(message.member.id);
if (!message.guild) return;
const args = message.content.slice(px.length).trim().split(/ +/g);
const cmd = args.shift().toLowerCase();
if (cmd.length === 0) return;
const command = client.commands.get(cmd) || client.commands.find((x) => x.aliases && x.aliases.includes(cmd));
if (command) {
if (command.timeout) {
if (Timeout.has(`${message.author.id}${command.name}`)) {
const embed = new MessageEmbed()
.setTitle('You are in timeout!')
.setColor(config.colour)
.setDescription(`:x: You need to wait **${humanizeDuration(command.timeout, { round: true })}** to use command again`)
.setColor('#ff0000')
return message.channel.send({ embeds: [embed] })
} else {
command.run(client, message, args);
Timeout.add(`${message.author.id}${command.name}`)
setTimeout(() => {
Timeout.delete(`${message.author.id}${command.name}`)
}, command.timeout);
}
} else {
command.run(client, message, args)
}
}
}
}
prefixSchema.js:
const mongoose = require('mongoose');
const serverSchema = new mongoose.Schema({
serverId: String,
prefix: String,
});
module.exports = mongoose.model('server', serverSchema);
我正在使用 Web 仪表板而不是自定义前缀命令来更改前缀,下面是将前缀保存到数据库的仪表板代码
仪表板代码
manager.js:
const Server = require('./serverModel');
module.exports = class Manager {
static async createServer(id) {
const result = new Server({
serverId: id,
prefix: null,
});
await result.save();
return result;
}
static async findServer(id) {
const result = await Server.findOne({ serverId: id });
return result;
}
static async getPrefix(id) {
const result = await Server.findOne({ serverId: id });
return result.prefix;
}
static async updateServerPrefix(id, prefix) {
const result = await Server.findOne({ serverId: id });
if (result) {
return await result.updateOne({ prefix });
}
else {
return;
}
}
};
serverModel.js:
const mongoose = require('mongoose');
const serverSchema = new mongoose.Schema({
serverId: String,
prefix: String,
});
module.exports = mongoose.model('server', serverSchema);
MongoDB Screenshot
您需要先从服务器初始化前缀。
const server = await db.servers.fetch({_id: serverId})
const prefix = server.prefix
您需要定义前缀并初始化它。
let prefix;
let data = await db.findOne({
guildID: message.guild.id
})
if(data === null) {
prefix = "."
} else {
prefix = data.Prefix
}