Reputation: 11
I'm having a problem with my Discord Bot where the only method of interfacing with the bot is by tagging it in a message. I've setup the code for how I thought the prefix functionality was supposed to work, but I've done something wrong. I don't get any error messages when in use and the bot functions perfectly fine if its tagged, but doesn't react at all if you try to use the prefix instead. Can someone explain please? (I'm using DSharp for the discord api)
Here's the exact part of the code I think there's a problem with:
var commandsConfig = new CommandsNextConfiguration
{
StringPrefixes = new List<string>() { configJson.Prefix },
EnableDms = false,
EnableMentionPrefix = true,
DmHelp = false,
EnableDefaultHelp = true,
UseDefaultCommandHandler = true
};
Console.WriteLine($"HELLO! {configJson.Prefix}"); //Testing it could read the desired prefix (it can)
Commands = Client.UseCommandsNext(commandsConfig);
The configJson.cs looks like this:
public struct ConfigJson
{
[JsonProperty("token")]
public string Token { get; private set; }
[JsonProperty("prefix")]
public string Prefix { get; private set; }
}
The config.Json file looks like this (The token is removed for security)
{
"token": "#######",
"prefix": "!"
}
I'll also post the entire .cs if you want to recreate the conditions
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using VLB.Commands.VLBCommands;
namespace VLB
{
public class Bot
{
public DiscordClient Client { get; private set; }
public InteractivityExtension Interactivity { get; private set; }
public CommandsNextExtension Commands { get; private set; }
public async Task RunAsync()
{
var json = string.Empty;
using (var fs = File.OpenRead("config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync().ConfigureAwait(false);
var configJson = JsonConvert.DeserializeObject<ConfigJson>(json);
var config = new DiscordConfiguration
{
Token = configJson.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
MinimumLogLevel = LogLevel.Debug,
};
Client = new DiscordClient(config);
Client.Ready += OnClientReady;
Client.UseInteractivity(new InteractivityConfiguration
{
Timeout = TimeSpan.FromSeconds(30)
});
var commandsConfig = new CommandsNextConfiguration
{
StringPrefixes = new List<string>() { configJson.Prefix },
EnableDms = false,
EnableMentionPrefix = true,
DmHelp = false,
EnableDefaultHelp = true,
UseDefaultCommandHandler = true
};
Console.WriteLine($"HELLO! {configJson.Prefix}"); //Testing it could read the desired prefix (it can)
Commands = Client.UseCommandsNext(commandsConfig);
Commands.RegisterCommands<VLBCommands>();
await Client.ConnectAsync();
await Task.Delay(-1);
}
private Task OnClientReady(DiscordClient client, ReadyEventArgs e)
{
return Task.CompletedTask;
}
}
}
I've tried changing the prefix to various conventional characters. I've also tried changing the assignment to "StringPrefixes" to a string array instead of a list.
I'm only trying to use one prefix, but after scouring DSharp documentation I'm not sure CommandsNextConfiguration has a way to indicate only one prefix.
Thanks for reading :)
Upvotes: 1
Views: 611