kyeede
kyeede
CC#
Created by kyeede on 2/23/2024 in #help
Circular Dependency
Heyo :D, So I have started getting into somewhat bigger projects again and needed to use Dependency Injections to loose coupling and overall tightness between classes. Since 2 of my classes (and likely in the future, more class depend on each other), it's a good approach to create abstractions to expose only a fraction of the class that other classes need to access. This way, I am not exposing it concretely. However, for some reason, I am running into a circular dependency exception which I don't seem to... really understand? I get where the issue is but I can't figure out a more plausible approach. I would appreciate some feedback on what potentially can be done. To give the issue.. ILoggingManager depends on IYukkaifyEnvironment, which in turn depends on ILoggingManager..
namespace Yukkaify
{
public interface IYukkaifyEnvironment
{
bool IsHooked { get; set; }
string Root { get; set; }
}

public interface IYukkaify
{
DiscordSocketClient Client { get; set; }
CommandService Command { get; set; }
IConfiguration Configuration { get; set; }
ILoggingManager LogManager { get; set; }
IGuildVerificationManager GuildManager { get; set; }
IEventsManager EventManager { get; set; }

Task InitializeYukkaify();
Task YukkaifyReadyHandler();
}

public class Yukkaify(DiscordSocketClient client, CommandService command, IConfiguration configuration, ILoggingManager logManager, IGuildVerificationManager guildManager, IEventsManager eventManager) : IYukkaify, IYukkaifyEnvironment
{
// Here I do whatever. Since nothing is being used atm besides ILoggingManager (im still working on it)
}
}
namespace Yukkaify
{
public interface IYukkaifyEnvironment
{
bool IsHooked { get; set; }
string Root { get; set; }
}

public interface IYukkaify
{
DiscordSocketClient Client { get; set; }
CommandService Command { get; set; }
IConfiguration Configuration { get; set; }
ILoggingManager LogManager { get; set; }
IGuildVerificationManager GuildManager { get; set; }
IEventsManager EventManager { get; set; }

Task InitializeYukkaify();
Task YukkaifyReadyHandler();
}

public class Yukkaify(DiscordSocketClient client, CommandService command, IConfiguration configuration, ILoggingManager logManager, IGuildVerificationManager guildManager, IEventsManager eventManager) : IYukkaify, IYukkaifyEnvironment
{
// Here I do whatever. Since nothing is being used atm besides ILoggingManager (im still working on it)
}
}
namespace Yukkaify.Utilities
{
public interface ILoggingManager
{
IYukkaifyEnvironment Yukkaify { get; }
IQueueManager QueueManager { get; }
IConfiguration Configuration { get; }
List<string> ContentCache { get; set; }

Task ContentLogger(LoggingManager.DebugType type, string message, Exception ex = null);
Task LogReporter();
}

public class LoggingManager(IYukkaifyEnvironment yukkaify, IQueueManager queueManager, IConfiguration configuration) : ILoggingManager
{
public IYukkaifyEnvironment Yukkaify { get; } = yukkaify;

public IQueueManager QueueManager { get; } = queueManager;

public IConfiguration Configuration { get; } = configuration;

public List<string> ContentCache { get; set; } = [];
}
}
namespace Yukkaify.Utilities
{
public interface ILoggingManager
{
IYukkaifyEnvironment Yukkaify { get; }
IQueueManager QueueManager { get; }
IConfiguration Configuration { get; }
List<string> ContentCache { get; set; }

Task ContentLogger(LoggingManager.DebugType type, string message, Exception ex = null);
Task LogReporter();
}

public class LoggingManager(IYukkaifyEnvironment yukkaify, IQueueManager queueManager, IConfiguration configuration) : ILoggingManager
{
public IYukkaifyEnvironment Yukkaify { get; } = yukkaify;

public IQueueManager QueueManager { get; } = queueManager;

public IConfiguration Configuration { get; } = configuration;

public List<string> ContentCache { get; set; } = [];
}
}
58 replies
CC#
Created by kyeede on 2/19/2024 in #help
Parse embed from Json into EmbedBuilderUtils
Hello! :D For my own convenience and to not clog up my entire class file with endless amounts of lines. I created a json file that holds numerous of preset embeds that I wanna use. However, I'm stuck on an issue and I certainly cannot seem to figure out the issue whatsoever.
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name",
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name",
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
i simply want to parse the embed into a utility that is named EmbedBuilderUtils which is provided by Discord.NET. It basically goes like this
_ = EmbedBuilderUtils.TryParse(Configuration.GetSection("embeds").GetValue<string>("first_embed"), out var builder);

await ReplyAsync(null, false, embed: builder.Build());
_ = EmbedBuilderUtils.TryParse(Configuration.GetSection("embeds").GetValue<string>("first_embed"), out var builder);

await ReplyAsync(null, false, embed: builder.Build());
This should technically speaking work? unless there is another way to parse first_embed's object entirely. I'm quite lost on this. Any help is appreciated!
38 replies
CC#
Created by kyeede on 2/17/2024 in #help
Access values from a record without explicitly calling .Value
To keep it quite short, I have a record as followed
public record ConfigurationValue<T>
{
public T Value { get; init; }
}
public record ConfigurationValue<T>
{
public T Value { get; init; }
}
Now when I wanna use it, I simply do something like
public ConfigurationValue<string> TestString { get; set; }
public ConfigurationValue<string> TestString { get; set; }
The annoying part is, upon deserializing it and wanting to access it, I need to do TestString.Value. Is there a way to change the code snippet to simply not use .Value at all? Quite new to records.
16 replies
CC#
Created by kyeede on 10/8/2023 in #help
❔ Creating a json array and reading a specific property
Hello! So i have a json file as followed
{
"embeds": [
{
"embed1": {
"title": "test number 1",
"description": "test",
"type": "rich",
"color": 79376
},
"embed2": {
"title": "test number 2",
"description": "test",
"type": "rich",
"color": 79376
},
"embed3": {
"title": "test number 3",
"description": "test",
"type": "rich",
"color": 79376
}
}
]
}
{
"embeds": [
{
"embed1": {
"title": "test number 1",
"description": "test",
"type": "rich",
"color": 79376
},
"embed2": {
"title": "test number 2",
"description": "test",
"type": "rich",
"color": 79376
},
"embed3": {
"title": "test number 3",
"description": "test",
"type": "rich",
"color": 79376
}
}
]
}
I want to read let's say everything from embed1 as if it was its own file. The thing to consider is that it has to be a string since the parameter is a string since im reading it like this
var parseObj = (JObject)JsonConvert.DeserializeObject($"App/Data/Configuration/embed.json");

EmbedBuilderUtils.TryParse(parseObj.SelectToken("embeds[0].embed1").ToObject<string>(), out var builder);
var parseObj = (JObject)JsonConvert.DeserializeObject($"App/Data/Configuration/embed.json");

EmbedBuilderUtils.TryParse(parseObj.SelectToken("embeds[0].embed1").ToObject<string>(), out var builder);
I am getting this error and idk what to do... Unexpected character encountered while parsing value: A. Path '', line 0, position 0..
35 replies
CC#
Created by kyeede on 7/13/2023 in #help
Foreach loop inside List<T> class
Okay so I'm actually quite confused on this. I have a collection of roles that I'd like to store inside a JSON file. My goal is to obtain every role's information within a guild by first obtaining the RoleCollection and then initiating a foreach loop to add it into a class list so later on I can serialize that information into a JSON file.
public class GuildInformation
{
public ulong GuildID { get; set; }
public string GuildName { get; set; }
public string GuildDescription { get; set; }
public string GuildAvatarUrl { get; set; }
public string GuildBannerUrl { get; set; }
public string GuildSplashUrl { get; set; }
public string GuildDiscoveryUrl { get; set; }
public string GuildVanityUrl { get; set; }
public string GuildCreationDate { get; set; }
public ulong GuildOwnerID { get; set; }
public string GuildOwnerName { get; set; }
public int GuildMfaLevel { get; set; }
public int GuildNsfwLevel { get; set; }
public int GuildVerificationLevel { get; set; }
public ulong GuildRulesChannel { get; set; }
public ulong GuildSafetyChannel { get; set; }
public ulong GuildPublicChannel { get; set; }
public ulong GuildModRuleChannel { get; set; }
public GuildPermissions GuildPermission { get; set; }
}

public class GuildPermissions
{
public List<GuildRolePermissions> GuildRolePermission { get; set; }
public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
}

public class GuildRolePermissions
{
public string GuildRoleName { get; set; }
public ulong GuildRoleID { get; set; }
public int GuildRolePermissionLevel { get; set; }
}

public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
{
var GuildRoleCollection = new List<GuildRolePermissions>();

var GuildInformation = new GuildInformation()
{
GuildID = GuildID,
GuildName = GuildName,
// etc...

GuildPermissions = new()
{
foreach (var GuildRole in Guild.Roles
{
var AddRole = new GuildRolePermission
{
GuildRoleName = GuildRole.Name,
GuildRoleID = GuildRole.Id,
GuildRolePermissionLevel = 0
}
};
GuildRoleCollection.Add(AddRole);
}
};
return GuildInformation;
}
public class GuildInformation
{
public ulong GuildID { get; set; }
public string GuildName { get; set; }
public string GuildDescription { get; set; }
public string GuildAvatarUrl { get; set; }
public string GuildBannerUrl { get; set; }
public string GuildSplashUrl { get; set; }
public string GuildDiscoveryUrl { get; set; }
public string GuildVanityUrl { get; set; }
public string GuildCreationDate { get; set; }
public ulong GuildOwnerID { get; set; }
public string GuildOwnerName { get; set; }
public int GuildMfaLevel { get; set; }
public int GuildNsfwLevel { get; set; }
public int GuildVerificationLevel { get; set; }
public ulong GuildRulesChannel { get; set; }
public ulong GuildSafetyChannel { get; set; }
public ulong GuildPublicChannel { get; set; }
public ulong GuildModRuleChannel { get; set; }
public GuildPermissions GuildPermission { get; set; }
}

public class GuildPermissions
{
public List<GuildRolePermissions> GuildRolePermission { get; set; }
public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
}

public class GuildRolePermissions
{
public string GuildRoleName { get; set; }
public ulong GuildRoleID { get; set; }
public int GuildRolePermissionLevel { get; set; }
}

public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
{
var GuildRoleCollection = new List<GuildRolePermissions>();

var GuildInformation = new GuildInformation()
{
GuildID = GuildID,
GuildName = GuildName,
// etc...

GuildPermissions = new()
{
foreach (var GuildRole in Guild.Roles
{
var AddRole = new GuildRolePermission
{
GuildRoleName = GuildRole.Name,
GuildRoleID = GuildRole.Id,
GuildRolePermissionLevel = 0
}
};
GuildRoleCollection.Add(AddRole);
}
};
return GuildInformation;
}
Is something like this possible? I'd like to do this way so upon the system detecting a new guild, it stores basic guild information and each role (to set up a permission-like system for commands)
34 replies
CC#
Created by kyeede on 4/1/2023 in #help
❔ Implement New Class to JSON with Default Values
Hello, I have a class as followed:
public class CommandModuleRoot
{
public List<GuildModulesConfiguration> GuildModulesConfiguration { get; set; }
}

public class GuildModulesConfiguration
{
public ModuleCaseSystem ModuleCaseSystem { get; set; }
}

public class ModuleCaseSystem
{
public int ModuleID { get; set; }
public string ModuleName { get; set; }
public bool ModuleIsActive{ get; set; }

[Description("Sets the channel where cases are automatically or manually created regardless in which channel the command was executed in.")]
public ulong GuildAutoPostChannel { get; set; }

[Description("Determines whether the user will be notified about any cases created for them. This also includes modifications to the case such as updates.")]
public bool EnableUserCaseNotifications { get; set; }
}
public class CommandModuleRoot
{
public List<GuildModulesConfiguration> GuildModulesConfiguration { get; set; }
}

public class GuildModulesConfiguration
{
public ModuleCaseSystem ModuleCaseSystem { get; set; }
}

public class ModuleCaseSystem
{
public int ModuleID { get; set; }
public string ModuleName { get; set; }
public bool ModuleIsActive{ get; set; }

[Description("Sets the channel where cases are automatically or manually created regardless in which channel the command was executed in.")]
public ulong GuildAutoPostChannel { get; set; }

[Description("Determines whether the user will be notified about any cases created for them. This also includes modifications to the case such as updates.")]
public bool EnableUserCaseNotifications { get; set; }
}
Which returns:
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
}
}
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
}
}
Now, let's say I add a new class to GuildModulesConfiguration which would looks like this:
public class GuildModulesConfiguration
{
public ModuleCaseSystem ModuleCaseSystem { get; set; }
public NewModuleTest NewModuleTest { get; set; }
}

public class NewModuleTest
{
public int ModuleID { get; set; } = 2;
public string ModuleName { get; set; } = "Test";
public bool ModuleIsActive { get; set; } = false;
}
public class GuildModulesConfiguration
{
public ModuleCaseSystem ModuleCaseSystem { get; set; }
public NewModuleTest NewModuleTest { get; set; }
}

public class NewModuleTest
{
public int ModuleID { get; set; } = 2;
public string ModuleName { get; set; } = "Test";
public bool ModuleIsActive { get; set; } = false;
}
Whenever I try to deserialize and serialize the JSON file, it adds the new class as null without any values.
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
},
"NewModuleTest": null
}
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
},
"NewModuleTest": null
}
What I want to achieve is this:
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
},
"NewModuleTest": {
"ModuleID": 2,
"ModuleName": "Test",
"ModuleIsActive": true
}
}
{
"ModuleCaseSystem": {
"ModuleID": 1,
"ModuleName": "CaseSystem",
"ModuleIsActive": false,
"GuildAutoPostChannel": 0,
"EnableUserCaseNotifications": false
},
"NewModuleTest": {
"ModuleID": 2,
"ModuleName": "Test",
"ModuleIsActive": true
}
}
Any ideas what I could possibly do? CryingMan
40 replies
CC#
Created by kyeede on 3/20/2023 in #help
Update JSON File with new values without overwriting existing data
Hello, So I'm currently developing a Discord bot that stores values such as 'settings' for each guild. I already got that part figured out very well but I would like to know how I should handle additions / removes. I have a class as followed:
public class GuildConfiguration
{
public string GuildPrefix { get; set; } = default!;
public bool EnableModeratorNotifications { get; set; } = default!;
public GuildCommandPermissions GuildCommandPermissions { get; set; } = default!;
}
public class GuildConfiguration
{
public string GuildPrefix { get; set; } = default!;
public bool EnableModeratorNotifications { get; set; } = default!;
public GuildCommandPermissions GuildCommandPermissions { get; set; } = default!;
}
As an example, currently, the json file doesn't have the public bool EnableModeratorNotifications included but it is a new feature I would like to add. If I add that to my class, how would I serialize it into the json file with a default value like true/false without overwriting the existing values there?
76 replies