0

我使用 MongoDB 作为 keyv 的存储适配器,将前缀设置为 后;,它再次重置为!. 据我说,逻辑上有错误。这是discord中使用的cmd https://cdn.discordapp.com/attachments/713041505719287818/902605670086508585/IMG_8303.png

const { Client, Intents, MessageEmbed, Collection } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES] });
const dotenv = require('dotenv');
const Keyv = require('keyv');
const keyv = new Keyv('mongodb://user:password@cluster0-shard-00-00.auifa.mongodb.net:27017,cluster0-shard-00-01.auifa.mongodb.net:27017,cluster0-shard-00-02.auifa.mongodb.net:27017/Discord?ssl=true&replicaSet=atlas-bs4dwb-shard-0&authSource=admin&retryWrites=true&w=majority', { collection: 'Discord' });
keyv.on('error', err => console.error('Keyv connection error:', err));
dotenv.config();
client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});
    
client.on('messageCreate', async (msg) => {
     if (msg.author.bot) return;
    let number = msg.content.split(' ')[1];
    if (msg.content === '!ping') {
        msg.channel.send('ping!')
    }
    
const prefixMap = await keyv.get('prefix');
 const getGuildPrefix = async () => {
        return prefixMap ?. [msg.guild.id] || "!"
    }

// Sets the prefix to the current guild.
    const setGuildPrefix = async (prefix) => {
        prefixMap[msg.guild.id] = prefix;
        await keyv.set('prefix', prefixMap);
    }
    let commandprefix = await getGuildPrefix();
// Get prefix command.
    if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${commandprefix}prefix`)) {
        msg.channel.send(`Your server prefix is ${commandprefix}`)
    }

// Change prefix command
    if ((msg.content.startsWith(`${process.env.prefix}setprefix`)) || (msg.content.startsWith(`${commandPrefix}setprefix`))) {
        const newPrefix = number;

        if (newPrefix.length === 0) {
            msg.channel.send(`Please enter a valid prefix`);
            return;
        }

        await setGuildPrefix(newPrefix)
        msg.channel.send(`Your server prefix is now '${newPrefix}'`);
    }
    })
client.login(process.env.token);

这段代码应该如何运行是:如果代码第一次在服务器上运行,那么前缀应该是!默认的,我们也可以将其更改为另一个前缀。这里的事情是它表明它改变了前缀但它没有更新数据库中的值

4

0 回答 0