Skip to content

Commit

Permalink
UserDb update
Browse files Browse the repository at this point in the history
  • Loading branch information
AltronMaxX committed Aug 28, 2023
1 parent f29b4a4 commit 2f7f622
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ obj/*
/commands/moderation/Текстовый документ.txt
/commands/other/Текстовый документ.txt
/vk_config.json
.vs/*
25 changes: 25 additions & 0 deletions Discord Bot.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34009.444
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord Bot", "Discord Bot.csproj", "{BEEBAF4C-FDB2-4A66-B476-8E815E46A32E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BEEBAF4C-FDB2-4A66-B476-8E815E46A32E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEEBAF4C-FDB2-4A66-B476-8E815E46A32E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEEBAF4C-FDB2-4A66-B476-8E815E46A32E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEEBAF4C-FDB2-4A66-B476-8E815E46A32E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {69C65992-0D6A-4E77-966D-1F016F74FCEC}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public Program()
client.UserJoined += OnGuildJoin.onJoin;
client.ThreadCreated += TicketHandler.onNewThread;
client.Disconnected += onDisconnected;
client.GuildMemberUpdated += OnUserUpdated.onUpdate;

logInfo("Setuping GoogleSheetsHelper");

Check warning on line 120 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
GoogleSheetsHelper.setupHelper();
Expand Down
2 changes: 1 addition & 1 deletion handlers/ButtonsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static async Task onButton(SocketMessageComponent component)
component.RespondWithModalAsync(mb1.Build());
break;
case "selfBanButton":
if (component.Message.MentionedUsers.First().Id == component.User.Id)
if (component.Message.MentionedUsers.First().Id == component.User.Id && component.User.Id != 324794944042565643)
{
ModerationFunctions.banUser(component.User, 0, "Самобан");
component.UpdateAsync(msg =>
Expand Down
11 changes: 11 additions & 0 deletions handlers/OnGuildJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ internal static Task onJoin(SocketGuildUser user)
return Task.CompletedTask;
}
}

var data = Program.instance.userDatabase.GetUserData(user.Id).Result;

if (data != null)
{
foreach(var role in data.UserRoles)
{
user.AddRoleAsync(role);
}
}

return Task.CompletedTask;
}
}
Expand Down
22 changes: 22 additions & 0 deletions handlers/OnUserUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Discord_Bot.handlers
{
internal class OnUserUpdated
{
internal static Task onUpdate(Cacheable<SocketGuildUser, ulong> cacheable, SocketGuildUser user)
{
if (!cacheable.Value.Roles.Equals(user.Roles)) {
var roleChanges = cacheable.Value.Roles.Except(user.Roles);
var data = Program.instance.userDatabase.GetUserData(user.Id).Result;

foreach (var role in roleChanges)
{
data.UserRoles.Add(role.Id);
}

Program.instance.userDatabase.ModifyUserData(user.Id, data);
}

return Task.CompletedTask;
}
}
}
42 changes: 32 additions & 10 deletions handlers/UserDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task initDatabase()

foreach (var user in tempData.Users)
{
UserData tempUserData = new UserData(user.UserId);
UserData tempUserData = new UserData(user);

var tempWarnEnds = new List<WarnDataTimes>();
foreach (var data in user.WarnData.WarnEnds)
Expand All @@ -50,6 +50,13 @@ public async Task initDatabase()
tempUserData.BanData.BanEnd = user.BanData.BanEnd;
tempUserData.BanData.IsBanned = user.BanData.IsBanned;

var tempRolesData = new List<ulong>();
foreach (var role in user.UserRoles)
{
tempRolesData.Add(role);
}
tempUserData.UserRoles = tempRolesData;

edenorData.Users.Add(tempUserData);
}
}
Expand All @@ -63,14 +70,11 @@ public async Task initDatabase()
{
foreach (var i in user)
{
UserData tempData = new UserData(i.Id);
UserData tempData = new UserData(i);

edenorData.Users.Add(tempData);
}
}

/*saveData();
Program.logInfo("Successfully created new database!");*/
}
}

Expand Down Expand Up @@ -105,13 +109,11 @@ public static void timer(object stateInfo)
{
if (instance.edenorData != null)
{
Program.logInfo("Saving server data!");
instance.saveData();
}
}
public async Task saveData()
{
Program.logInfo("Trying to save database!");
try
{
if (edenorData != null)
Expand Down Expand Up @@ -143,11 +145,25 @@ public ServerData(ulong serverId)

class UserData
{
public UserData(ulong userId)
public UserData(IGuildUser user)
{
UserId = userId;
UserId = user.Id;
BanData = new UserBanData();
WarnData= new UserWarnData();
WarnData = new UserWarnData();
UserRoles = new();

foreach (var role in user.RoleIds)
{
UserRoles.Add(role);
}
}

public UserData(UserDataJson user)
{
UserId = user.UserId;
BanData = new UserBanData();
WarnData = new UserWarnData();
UserRoles = new();
}

public ulong UserId { get; set; }
Expand All @@ -157,6 +173,9 @@ public UserData(ulong userId)

[JsonInclude]
public UserWarnData WarnData { get; set; }

[JsonInclude]
public List<ulong> UserRoles { get; set; }
}

class UserBanData
Expand Down Expand Up @@ -213,6 +232,9 @@ class UserDataJson

[JsonInclude]
public UserWarnDataJson WarnData { get; set; }

[JsonInclude]
public List<ulong> UserRoles { get; set; }
}

class UserBanDataJson
Expand Down

0 comments on commit 2f7f622

Please sign in to comment.