Answer the question
In order to leave comments, you need to log in
How to change the prefix of a discord bot with a command using the discord.js library?
How to change the bot prefix with a command on the discord server? Like +prefix !. And then the bot prefix is !. My main file:
const Discord = require('discord.js')
const fs = require('fs')
const client = new Discord.Client()
const config = require('./config.json')
const fetch = require('node-fetch')
client.commands = new Discord.Collection()
fs.readdir('./commands', (err, files) => {
if (err) console.log(err)
let jsfile = files.filter(f => f.split('.').pop() === 'js')
if (jsfile.length <= 0) return message.channel.send("Команды не найдены!")
console.log(`Загружено ${jsfile.length} команд`)
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`)
client.commands.set(props.help.name, props)
})
})
client.on('ready', () => {
console.log(`Бот ${client.user.username} запустился`);
client.user.setPresence({
status: 'online',
activity: {
type: 'STREAMING',
name: 'YouTube',
}
})
})
client.on('message', message => {
let prefix = config.prefix
let guildPrefix = prefix.getPrefix(message.guild.id);
if (!guildPrefix) guildPrefix = prefix;
if (!message.content.startsWith(prefix) || message.author.bot) return;
let messageArray = message.content.split(' ')
let command = messageArray[0]
let args = messageArray.slice(1)
let command_file = client.commands.get(command.slice(prefix.length))
if (command_file) command_file.run(client, message, args, prefix)
})
client.login(config.token)
Answer the question
In order to leave comments, you need to log in
And now the easiest and I have a working way: create a prefixes.json file in the bot folder. Then, in the main bot file, add the following to the base:
// вверху со всеми импортами
let fs = require("fs")
let prefixes = require("./prefixes.json")
// ниже
let sid = message.guild.id
if(!prefixes[sid]){
prefixes[sid] ={
prefix:"любой префикс по умолчанию",
};
};
let prefix = prefixes[sid].prefix
fs.writeFile("./prefixes.json",JSON.stringify(prefixes),(err)=>{
if (err) console.log(err)
}
// вверху со всеми импортами
let fs = require("fs")
let prefixes = require("./prefixes.json")
// ниже
let sid = message.guild.id
prefixes[sid] ={
prefix:args[0]
}
fs.writeFile("./prefixes.json",JSON.stringify(prefixes),(err)=>{
if (err) console.log(err)
}
Almost a direct neighboring question How to change the bot config with commands in Discord?
Just change in your config
let prefix = '!';
let currencySymbol = '@';
discordBot.on('message', (msg) => {
let [ command, ...args ] = msg.content.split(' ');
switch(true) {
case(command === `{ $prefix }set-prefix`):
prefix = args[0] ?? prefix;
break;
case(command === `{ $prefix }set-currency`):
currencySymbol = args[0] ?? currencySymbol;
break;
default: break;
};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question