I use MongoDB as a storage adapter for keyv, After setting the prefix to ;
, it again resets to !
. according to me there is a error in the logic. Here is the cmd used in discord
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:[email protected]: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);
How this code is supposed to run is: If the code is run for the first time in the server, then the prefix is supposed to be !
as default, we can also change it to another prefix. The thing here is it shows that it changed the prefix but it didn't update the value in the database