#Custom Prefix

1 messages · Page 1 of 1 (latest)

lavish crag
#

I think I can remember registering some commands for specific servers only, do maybe you could register your own commands with custom prefixes for the specific server.

Or just write your own command parser, in the easiest case with the Message Created Event

dense bobcat
hybrid drum
#

You can replace the default prefix resolver

dense bobcat
# lavish crag I think I can remember registering some commands for specific servers only, do m...
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
using MongoDB.Driver;
using Violet.commands;
using Violet.config;
using VioletClient;

namespace Violet.handlers
{
    public class CommandsHandler
    {
        public static async Task Client_Message(DiscordClient sender, MessageCreateEventArgs args)
        {
            var collection = Program._mongoDbService?.GetCollection<GuildDocument>("guilds");

            var filter = Builders<GuildDocument>.Filter.Eq("_id", args.Guild.Id.ToString());
            var guildDocument = await collection.Find(filter).FirstOrDefaultAsync();

            if (guildDocument == null)
            {
                guildDocument = new GuildDocument
                {
                    _id = args.Guild.Id.ToString(),
                };

                await collection!.InsertOneAsync(guildDocument);
            }

            var commandsConfig = new CommandsNextConfiguration()
            {
                StringPrefixes = new string[] { guildDocument.prefix! },
                EnableMentionPrefix = true,
                EnableDms = true,
                EnableDefaultHelp = false
            };

            var commands = sender.UseCommandsNext(commandsConfig);

            commands.RegisterCommands<CommonCommands>();
            commands.RegisterCommands<EconomyCommands>();
        }
    }
}``` I tried and unfortunately it didn't work, it says "CommandsNext is already enabled for that client.".
lavish crag
#

I meant instead of the Command register

#

#1262242571921395835 message

I also didn't know that was possible, so I think that's the way to go

dense bobcat
lavish crag
#

I never did it before, but I assume there is an abstract base class in which you can implement all of the features.
Pretty sure one of the lib devs can help you

south snowBOT
#

DSharpPlus.CommandsNext.PrefixResolverDelegate

Summary

Represents a delegate for a function that takes a message, and returns the position of the start of command invocation in the message. It has to return -1 if prefix is not present.

It is recommended that helper methods and
be used internally for checking. Their output can be passed through.

Declaration

public sealed class PrefixResolverDelegate : MulticastDelegate, ICloneable, ISerializable;
iron sinew
#

you can then pass this to your CommandsNextConfiguration

#

(it's a delegate, don't let the bot output confuse you)

dense bobcat
#

ok

dense bobcat
#

Don't have this option yet?

dense bobcat
#
using System.Text;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using MongoDB.Driver;
using Violet.commands;
using VioletClient;

namespace Violet.handlers
{
    public class CommandsHandler
    {
        public static void RegisterCommands(DiscordClient discord)
        {
            var commandsConfig = new CommandsNextConfiguration()
            {
                PrefixResolver = ResolvePrefixAsync,
                EnableMentionPrefix = true,
                EnableDms = true,
                EnableDefaultHelp = false
            };

            var commands = discord.UseCommandsNext(commandsConfig);

            commands.RegisterCommands<CommonCommands>();
            commands.RegisterCommands<EconomyCommands>();
        }

        private static async Task<int> ResolvePrefixAsync(DiscordMessage msg)
        {
            if (msg.Channel.Guild != null!)
            {
                var guildId = msg.Channel.Guild.Id;
                var prefix = await GetPrefixAsync(guildId);

                if (!string.IsNullOrEmpty(prefix))
                {
                    var bytes = Encoding.UTF8.GetBytes(prefix);
                    return bytes.Length;
                }
            }

            return 0;
        }

        private static async Task<string> GetPrefixAsync(ulong guildId)
        {
            try
            {
                var collection = Program._mongoDbService?.GetCollection<GuildDocument>("guilds");

                var filter = Builders<GuildDocument>.Filter.Eq("_id", guildId.ToString());
                var guildDocument = await collection.Find(filter).FirstOrDefaultAsync();

                return guildDocument?.prefix ?? "v";
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Erro ao obter prefixo do servidor: {ex.Message}");
                return "v";
            }
        }
    }
}``` @iron sinew can you help me?
iron sinew
#

not if you don't state what the problem is, what you expected to happen, etc

dense bobcat
iron sinew
#

have you made sure to carefully check the documentation:

It has to return -1 if prefix is not present.

#

also this code is bad and will break on non-ASCII prefixes

#

just return prefix.Length

#

from there i also suggest you actually check whether the prefix is present

#

furthermore, please don't ping individuals for support - we're all volunteers and we'll get around to it whenever we have time