Skip to content

Commit

Permalink
fix null prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Dec 3, 2023
1 parent 9e0ef48 commit a2caf4a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Mewdeko.Database/Models/GuildConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mewdeko.Database.Models;
public class GuildConfig : DbEntity
{
public ulong GuildId { get; set; }
public string? Prefix { get; set; } = null;
public string? Prefix { get; set; } = "";
public ulong StaffRole { get; set; }
public ulong GameMasterRole { get; set; }
public ulong CommandLogChannel { get; set; } = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko/Common/Configs/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public BotConfig()
ConsoleOutputType = ConsoleOutputType.Normal;
ForwardMessages = false;
ForwardToAllOwners = false;
DmHelpText = @"{""description"": ""Type `%prefix%h` for help.""}";
DmHelpText = """{"description": "Type `%prefix%h` for help."}""";
HelpText = @"change this in bot.yml";
Blocked = new BlockedConfig();
Prefix = ".";
Expand Down
5 changes: 2 additions & 3 deletions src/Mewdeko/Mewdeko.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ private async Task AddServices()
AllGuildConfigs =
new ConcurrentHashSet<GuildConfig>(
uow.GuildConfigs.GetAllGuildConfigs(Client.Guilds.Select(x => x.Id).ToList()));
var guildSettingsService = new GuildSettingsService(db, null, this);
await uow.EnsureUserCreated(bot.Id, bot.Username, bot.Discriminator, bot.AvatarId);
gs2.Stop();
Log.Information("Guild Configs cached in {ElapsedTotalSeconds}s", gs2.Elapsed.TotalSeconds);
Expand All @@ -112,7 +111,6 @@ private async Task AddServices()
.AddSingleton(Cache)
.AddSingleton(new MartineApi())
.AddSingleton(Cache.Redis)
.AddSingleton(guildSettingsService)
.AddTransient<ISeria, JsonSeria>()
.AddTransient<IPubSub, RedisPubSub>()
.AddTransient<IConfigSeria, YamlSeria>()
Expand All @@ -139,7 +137,8 @@ private async Task AddServices()
DisconnectOnStop = false
})
.AddScoped<ISearchImagesService, SearchImagesService>()
.AddSingleton<ToneTagService>();
.AddSingleton<ToneTagService>()
.AddSingleton<GuildSettingsService>();
if (Credentials.UseGlobalCurrency)
{
s.AddTransient<ICurrencyService, GlobalCurrencyService>();
Expand Down
3 changes: 3 additions & 0 deletions src/Mewdeko/Services/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ private async Task TryRunCommand(IGuild? guild, IChannel channel, IUserMessage u

var prefix = await gss.GetPrefix(guild?.Id);

if (prefix is null /*somehow*/)
return;

var startsWithPrefix = messageContent.StartsWith(prefix, StringComparison.InvariantCulture);
var startsWithBotMention =
messageContent.StartsWith($"<@{client.CurrentUser.Id}> ", StringComparison.InvariantCulture) ||
Expand Down
7 changes: 5 additions & 2 deletions src/Mewdeko/Services/GuildSettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Mewdeko.Services.Settings;
using Microsoft.Extensions.DependencyInjection;

namespace Mewdeko.Services;

public class GuildSettingsService(DbService db, IConfigService? bss, Mewdeko bot) : INService
public class GuildSettingsService(DbService db, IConfigService? bss, Mewdeko bot, IServiceProvider services)
{
public async Task<string> SetPrefix(IGuild guild, string prefix)
{
Expand All @@ -20,9 +21,11 @@ public async Task<string> SetPrefix(IGuild guild, string prefix)

public async Task<string?> GetPrefix(ulong? id)
{
bss = services.GetRequiredService<BotConfigService>();
if (!id.HasValue)
return bss.GetSetting("prefix");
return (await GetGuildConfig(id.Value)).Prefix ??= bss.GetSetting("prefix");
var prefix = (await GetGuildConfig(id.Value)).Prefix;
return string.IsNullOrWhiteSpace(prefix) ? bss.GetSetting("prefix") : prefix;
}

public Task<string?> GetPrefix() => Task.FromResult(bss.GetSetting("prefix"));
Expand Down

0 comments on commit a2caf4a

Please sign in to comment.