Skip to content

Commit

Permalink
[Bot -> Library] Create constants class.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogFeelings committed Dec 28, 2023
1 parent 1635ff6 commit a436eaf
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 108 deletions.
3 changes: 2 additions & 1 deletion SammBot.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CONTINUOUS_INDENT_MULTIPLIER/@EntryValue">2</s:Int64>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderRegionName/@EntryValue">License Information (GPLv3)</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Samm-Bot - A lightweight Discord.NET bot for moderation and other purposes.
Copyright (C) 2021-$CURRENT_YEAR$ Analog Feelings
Copyright (C) 2021-${CurrentDate.Year} Analog Feelings

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -25,6 +25,7 @@ Copyright (C) 2021-$CURRENT_YEAR$ Analog Feelings

You should have received a copy of the GNU General Public License
along with this program. If not, see &lt;https://www.gnu.org/licenses/&gt;.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
50 changes: 0 additions & 50 deletions Source/SammBot.Bot/Core/BotGlobals.cs

This file was deleted.

14 changes: 8 additions & 6 deletions Source/SammBot.Bot/Core/MainProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
#endregion

global using LogSeverity = Matcha.LogSeverity;

using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -30,14 +32,14 @@
using Discord.Interactions;
using Fergun.Interactive;
using SammBot.Bot.Services;
using SammBot.Library;
using SammBot.Library.Helpers;
using System.Text.Json;

namespace SammBot.Bot.Core;

/// <summary>
/// A class containing all the startup logic
/// for the bot.
/// A class containing all of the startup logic.
/// </summary>
public class MainProgram
{
Expand All @@ -46,6 +48,8 @@ public class MainProgram

public static void Main()
{
Constants.RuntimeStopwatch.Start();

MainProgram mainProgram = new MainProgram();

AsyncHelper.RunSync(() => mainProgram.MainAsync());
Expand All @@ -58,8 +62,6 @@ private async Task MainAsync()
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;

BotGlobals.Instance.RuntimeStopwatch.Start();

BootLogger bootLogger = new BootLogger();

bootLogger.Log($"Loading {SettingsManager.CONFIG_FILE}...", LogSeverity.Information);
Expand All @@ -68,10 +70,10 @@ private async Task MainAsync()
string fullPath = SettingsManager.Instance.BotDataDirectory;
string serializedSettings = JsonSerializer.Serialize(SettingsManager.Instance.LoadedConfig, BotGlobals.Instance.JsonSettings);

Check failure on line 71 in Source/SammBot.Bot/Core/MainProgram.cs

View workflow job for this annotation

GitHub Actions / run-unit-tests (Debug, 8.0.x)

The name 'BotGlobals' does not exist in the current context

Check failure on line 71 in Source/SammBot.Bot/Core/MainProgram.cs

View workflow job for this annotation

GitHub Actions / run-unit-tests (Debug, 8.0.x)

The name 'BotGlobals' does not exist in the current context

Check failure on line 71 in Source/SammBot.Bot/Core/MainProgram.cs

View workflow job for this annotation

GitHub Actions / run-unit-tests (Release, 8.0.x)

The name 'BotGlobals' does not exist in the current context

Check failure on line 71 in Source/SammBot.Bot/Core/MainProgram.cs

View workflow job for this annotation

GitHub Actions / run-unit-tests (Release, 8.0.x)

The name 'BotGlobals' does not exist in the current context

bootLogger.Log($"Could not load {SettingsManager.CONFIG_FILE} correctly! Make sure the path \"{fullPath}\" exists.\n" +
bootLogger.Log($"Could not load {SettingsManager.CONFIG_FILE} correctly! Make sure the path \"{Constants.BotDataDirectory}\" exists.\n" +
$"A template {SettingsManager.CONFIG_FILE} file has been written to that path.", LogSeverity.Fatal);

await File.WriteAllTextAsync(Path.Combine(fullPath, SettingsManager.CONFIG_FILE), serializedSettings);
await File.WriteAllTextAsync(Path.Combine(Constants.BotDataDirectory, Constants.CONFIG_FILE), serializedSettings);

bootLogger.Log("Press any key to exit...", LogSeverity.Information);
Console.ReadKey();
Expand Down
4 changes: 2 additions & 2 deletions Source/SammBot.Bot/Modules/BooruModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class BooruModule : InteractionModuleBase<ShardedInteractionContext>
replyEmbed.WithUrl($"https://rule34.xxx/index.php?page=post&s=view&id={filteredPosts[0].Id}");
replyEmbed.WithImageUrl(filteredPosts[0].FileUrl);

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);
}
else
{
Expand Down Expand Up @@ -180,7 +180,7 @@ PageBuilder GeneratePage(int Index)
replyEmbed.WithUrl($"https://e621.net/posts/{post.Id}");
replyEmbed.WithImageUrl(post.File.FileUrl);

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions Source/SammBot.Bot/Modules/BotAdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<RuntimeResult> ServersAsync()

builtMessage += Format.Code(codeBlock);

await RespondAsync(builtMessage, ephemeral: true, allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(builtMessage, ephemeral: true, allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand All @@ -67,7 +67,7 @@ public async Task<RuntimeResult> ServersAsync()
[HideInHelp]
public async Task<RuntimeResult> ShutdownAsync()
{
await RespondAsync($"{SettingsManager.BOT_NAME} will shut down.", ephemeral: true, allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync($"{SettingsManager.BOT_NAME} will shut down.", ephemeral: true, allowedMentions: Constants.AllowOnlyUsers);

Logger.Log($"{SettingsManager.BOT_NAME} will shut down.\n\n", LogSeverity.Warning);

Expand All @@ -89,7 +89,7 @@ public async Task<RuntimeResult> LeaveAsync([Summary(description: "The ID of the
string targetGuildName = targetGuild.Name;
await targetGuild.LeaveAsync();

await RespondAsync($"Left the server \"{targetGuildName}\".", ephemeral: true, allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync($"Left the server \"{targetGuildName}\".", ephemeral: true, allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down
18 changes: 9 additions & 9 deletions Source/SammBot.Bot/Modules/FunModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<RuntimeResult> MagicBallAsync([Summary(description: "The quest
string chosenAnswer = _MagicBallAnswers.PickRandom();

await RespondAsync($":8ball: Asking the magic 8-ball...\n" +
$"**• Question**: {Question}", allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
$"**• Question**: {Question}", allowedMentions: Constants.AllowOnlyUsers);

await Task.Delay(2000);

Expand All @@ -109,7 +109,7 @@ public async Task<RuntimeResult> CreateActivityAsync([Summary(description: "The
IInviteMetadata invite = await author.VoiceChannel.CreateInviteToApplicationAsync(ActivityType);

await RespondAsync($":warning: **Most activities only work if the server has a Nitro Boost level of at least 1.**\n\n" +
$"{invite.Url}", allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
$"{invite.Url}", allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand All @@ -124,7 +124,7 @@ public async Task<RuntimeResult> RollDiceAsync([Summary(description: "The amount

int chosenNumber = Random.Shared.Next(1, FaceCount + 1);

await RespondAsync(":game_die: Rolling the die...", allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(":game_die: Rolling the die...", allowedMentions: Constants.AllowOnlyUsers);

await Task.Delay(1500);

Expand All @@ -144,7 +144,7 @@ public async Task<RuntimeResult> HugUserAsync([Summary(description: "The user yo
SocketGuildUser authorGuildUser = (Context.Interaction.User as SocketGuildUser)!;

await RespondAsync($"Warm hugs from **{authorGuildUser.GetUsernameOrNick()}**!\n{chosenKaomoji} <@{User.Id}>",
allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand All @@ -157,7 +157,7 @@ public async Task<RuntimeResult> PatUserAsync([Summary(description: "The user yo
{
SocketGuildUser authorGuildUser = (Context.Interaction.User as SocketGuildUser)!;

await RespondAsync($"Pats from **{authorGuildUser.GetUsernameOrNick()}**!\n(c・_・)ノ”<@{User.Id}>", allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync($"Pats from **{authorGuildUser.GetUsernameOrNick()}**!\n(c・_・)ノ”<@{User.Id}>", allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand All @@ -174,7 +174,7 @@ public async Task<RuntimeResult> DoxUserAsync([Summary(description: "The user yo
int fourthSegment = Random.Shared.Next(0, 256);

await RespondAsync($"**{User.GetUsernameOrNick()}**'s IPv4 address: `{firstSegment}.{secondSegment}.{thirdSegment}.{fourthSegment}`",
allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand All @@ -191,7 +191,7 @@ public async Task<RuntimeResult> FirstDegreeMurderAsync([Summary(description: "T
chosenMessage = chosenMessage.Replace("{Murderer}", $"**{authorUser.GetUsernameOrNick()}**");
chosenMessage = chosenMessage.Replace("{Victim}", $"**{TargetUser.GetUsernameOrNick()}**");

await RespondAsync(chosenMessage, allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(chosenMessage, allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public async Task<RuntimeResult> ShipUsersAsync([Summary(description: "The first

// Use SendFileAsync to be able to upload the stream to Discord's servers. The file name has to be the same as the one set in ImageUrl.
await FollowupWithFileAsync(finalImageStream, "shipImage.png", preEmbedText,
embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);
}
}

Expand Down Expand Up @@ -468,7 +468,7 @@ public async Task<RuntimeResult> UrbanAsync([Summary(description: "The term you

replyEmbed.WithUrl(chosenDefinition.Permalink);

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/SammBot.Bot/Modules/GuildConfigModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task<RuntimeResult> ListSettingsAsync()
}
}

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public async Task<RuntimeResult> SetSettingAsync([Summary(description: "The name
EmbedBuilder replyEmbed = new EmbedBuilder().BuildSuccessEmbed(Context)
.WithDescription($"Successfully set setting **{SettingName}** to value `{newValue}`.");

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SammBot.Bot/Modules/HelpModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public async Task<RuntimeResult> HelpAsync([Summary(description: "Leave empty to
}
}

await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await FollowupAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down
12 changes: 6 additions & 6 deletions Source/SammBot.Bot/Modules/InformationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public async Task<RuntimeResult> InformationFullAsync()
EmbedBuilder replyEmbed = new EmbedBuilder().BuildDefaultEmbed(Context);

string elapsedUptime = string.Format("{0:00} days,\n{1:00} hours,\n{2:00} minutes",
BotGlobals.Instance.RuntimeStopwatch.Elapsed.Days,
BotGlobals.Instance.RuntimeStopwatch.Elapsed.Hours,
BotGlobals.Instance.RuntimeStopwatch.Elapsed.Minutes);
Constants.RuntimeStopwatch.Elapsed.Days,
Constants.RuntimeStopwatch.Elapsed.Hours,
Constants.RuntimeStopwatch.Elapsed.Minutes);
long memoryUsage = Process.GetCurrentProcess().PrivateMemorySize64 / 1000000;

#if DEBUG
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task<RuntimeResult> InformationFullAsync()
replyEmbed.AddField("\U0001f5a5\uFE0F Host System", FriendlySystemName(), true);
replyEmbed.AddField("\U0001f4ca Working Set", $"{memoryUsage} megabytes", true);

await RespondAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down Expand Up @@ -134,7 +134,7 @@ public async Task<RuntimeResult> ServerInfoAsync()
replyEmbed.AddField("\U0001f465 Member Count", memberCount, true);
replyEmbed.AddField("\U0001f4e6 Role Count", roleCount, true);

await RespondAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public async Task<RuntimeResult> UserInfoAsync([Summary(description: "The user y
replyEmbed.AddField("\U0001f680 Booster Since", boostingSince, true);
replyEmbed.AddField("\U0001f4e6 Roles", roles);

await RespondAsync(embed: replyEmbed.Build(), allowedMentions: BotGlobals.Instance.AllowOnlyUsers);
await RespondAsync(embed: replyEmbed.Build(), allowedMentions: Constants.AllowOnlyUsers);

return ExecutionResult.Succesful();
}
Expand Down
Loading

0 comments on commit a436eaf

Please sign in to comment.