#Custom Prefix
1 messages · Page 1 of 1 (latest)
Hm, Ok, I'll test here to see if it works with message create, as I need this custom prefix on any server, as some owners like to modify the default prefix.
You can replace the default prefix resolver
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.".
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
hm, and how can I change this prefix resolver? because to get the prefix of the current server I need the ID of the server that the command was used, sorry I'm new to C#.
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
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;
you can then pass this to your CommandsNextConfiguration
(it's a delegate, don't let the bot output confuse you)
ok
I managed to solve it, now what I'm in doubt about is how can I put a button in a message without needing an embed.
Don't have this option yet?
Constructs a Message to be sent.
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?
not if you don't state what the problem is, what you expected to happen, etc
I made this command handler but when I set the prefix "v" in the database, it responds to commands with any prefix: "ping" or even ".ping" I want it to return the command only with "v" or whatever the server owner set.
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