MessageCreate.js 数据库前缀问题 - Discord.js v13/MongoDB

Posted

技术标签:

【中文标题】MessageCreate.js 数据库前缀问题 - Discord.js v13/MongoDB【英文标题】:MessageCreate.js prefix issues with database - Discord.js v13/MongoDB 【发布时间】:2021-11-13 22:32:08 【问题描述】:

我的 messageCreate 事件中有此代码:

// MODULES
const Discord = require('discord.js');
const mongoose = require('mongoose');
const Levels = require('discord.js-leveling');

// FILES
const Guild = require('../../models/guild');
const config = require('../../files/config.json');
const swearwords = require("../../files/data.json");
const colors = require('../../files/colors.json');

// ERROR MESSAGE

const errorMain = new Discord.MessageEmbed()
    .setDescription("There was an error!")
    .setColor(colors.COLOR)
const addedDatabase = new Discord.MessageEmbed()
    .setDescription("This server is now added to our database.")
    .setColor(colors.COLOR)

module.exports = async (Discord, client, message) => 

    if (!message.guild) return;
    if (message.author.bot) return;

    const settings = await Guild.findOne(
        guildID: message.guild.id
    , (err, guild) => 
        if (err) message.channel.send(errorMain);
        if (!guild) 
            const newGuild = new Guild(
                _id: mongoose.Types.ObjectId(),
                guildID: message.guild.id,
                prefix: config.PREFIX,
                logChannelID: String,
                enableLog: false,
                enableSwearFilter: true,
                enableMusic: true,
                enableLevel: true,
            );

            newGuild.save()
                .catch(err => message.channel.send(errorMain));
            return message.channel.send(embeds: [addedDatabase]).then(m => m.delete( timeout: 10000 ))
        
    );

    // VARIABLES
    const IDGuild = message.guild.id;
    const user = message.author;
    const prefix = settings.prefix;
    const swearFilterOn = settings.enableSwearFilter;

    // LEVEL SYSTEM
    if (settings.enableLevel === "true") 

        const requiredXp = Levels.xpFor(parseInt(user.level) + 1)

        const randomAmountOfXp = Math.floor(Math.random() * 29) + 1;
        const hasLeveledUp = await Levels.appendXp(message.author.id, message.guild.id, randomAmountOfXp);

        if (hasLeveledUp) 

            const user = await Levels.fetch(message.author.id, message.guild.id);

            const levelEmbed = new Discord.MessageEmbed()
                .setTitle('New Level!')
                .setColor(colors.COLOR)
                .setDescription(`**GG** $message.author, you just leveled up to level **$user.level**!\nContiune to chat to level up again.`)

            const sendEmbed = await message.channel.send(embeds: [levelEmbed]);
        
    

    // EXECUTE COMMAND AND SWEARFILTER
    if (swearFilterOn === "true") 
        var msg = message.content.toLowerCase();
        for (let i = 0; i < swearwords["swearwords"].length; i++) 
            if (msg.includes(swearwords["swearwords"][i])) 
                message.delete();
                return message.channel.send("Please do not swear.").then(msg => msg.delete( timeout: 3000 ));
            
        
        if (!message.content.startsWith(prefix) || message.author.bot) return;

        const args = message.content.slice(prefix.length).split(/ +/);
        const cmd = args.shift().toLowerCase();

        const command = client.commands.get(cmd) ||
            client.commands.find(a => a.aliases && a.aliases.includes(cmd));;

        if (command) command.execute(client, message, args, Discord)

     else 
        if (!message.content.startsWith(prefix) || message.author.bot) return;

        const args = message.content.slice(prefix.length).split(/ +/);
        const cmd = args.shift().toLowerCase();

        const command = client.commands.get(cmd) ||
            client.commands.find(a => a.aliases && a.aliases.includes(cmd));;

        if (command) command.execute(client, message, args, Discord)
    

当我在机器人加入不和谐之后立即发送消息时,它会给出错误,即它无法读取 null 的属性(读取“前缀”)。 机器人崩溃了,当它重新启动时它可以工作,因为它已被添加到数据库中。那么当它发送第一条消息时我该如何解决它仍然可以读取“前缀”。可能是数据库问题。

有人告诉我 settings.prefix 可能没有赋值,有没有办法解决这个问题? 我正在为我的数据库使用 mongodb。

【问题讨论】:

【参考方案1】:

您确实应该在机器人通过guildCreate 事件加入新服务器后立即分配前缀。如果您的数据库关闭等可能会导致一些问题。当您等待消息事件时

【讨论】:

以上是关于MessageCreate.js 数据库前缀问题 - Discord.js v13/MongoDB的主要内容,如果未能解决你的问题,请参考以下文章

数据库列类型前缀

表的 ORM 和前缀

在 PostgreSQL 数据库中搜索前缀

允许用户删除拥有的前缀数据库

如何在整个 MySQL 脚本上假设一个表前缀?

一张表的不同表前缀?