V
V
Vlad Morkovkin2021-06-08 17:57:47
Node.js
Vlad Morkovkin, 2021-06-08 17:57:47

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

3 answer(s)
A
AlmondPark33609, 2021-06-20
@Vlad808

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)

}

ALWAYS enter {} in the prefixes.json file and save before launching! The rest will be entered automatically.
Next, create a command (I have it in a separate file), add to the base:
// вверху со всеми импортами
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)
}

That's all ;) now we can change the prefix with a command. I think you will understand the name of the command and how to issue it) Also, in the main file, do not forget to remove the import of the prefix from the bot config.

H
hto_on, 2021-06-08
@hto_on

In the config.json file, the prefix line

A
Alexey, 2021-06-08
@Azperin

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 question

Ask a Question

731 491 924 answers to any question