diff --git a/.gitignore b/.gitignore index 98e8fdd..5ee85b9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ obj/* /commands/moderation/Текстовый документ.txt /commands/other/Текстовый документ.txt /vk_config.json +.vs/* \ No newline at end of file diff --git a/Discord Bot.sln b/Discord Bot.sln new file mode 100644 index 0000000..61fb697 --- /dev/null +++ b/Discord Bot.sln @@ -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 diff --git a/Program.cs b/Program.cs index 748fe9e..abb552e 100644 --- a/Program.cs +++ b/Program.cs @@ -115,6 +115,7 @@ public Program() client.UserJoined += OnGuildJoin.onJoin; client.ThreadCreated += TicketHandler.onNewThread; client.Disconnected += onDisconnected; + client.GuildMemberUpdated += OnUserUpdated.onUpdate; logInfo("Setuping GoogleSheetsHelper"); GoogleSheetsHelper.setupHelper(); diff --git a/handlers/ButtonsHandler.cs b/handlers/ButtonsHandler.cs index 2d27769..1e4fc21 100644 --- a/handlers/ButtonsHandler.cs +++ b/handlers/ButtonsHandler.cs @@ -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 => diff --git a/handlers/OnGuildJoin.cs b/handlers/OnGuildJoin.cs index c0d0bbb..793bf7e 100644 --- a/handlers/OnGuildJoin.cs +++ b/handlers/OnGuildJoin.cs @@ -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; } } diff --git a/handlers/OnUserUpdated.cs b/handlers/OnUserUpdated.cs new file mode 100644 index 0000000..931a680 --- /dev/null +++ b/handlers/OnUserUpdated.cs @@ -0,0 +1,22 @@ +namespace Discord_Bot.handlers +{ + internal class OnUserUpdated + { + internal static Task onUpdate(Cacheable 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; + } + } +} \ No newline at end of file diff --git a/handlers/UserDatabase.cs b/handlers/UserDatabase.cs index 8e26cce..a91a468 100644 --- a/handlers/UserDatabase.cs +++ b/handlers/UserDatabase.cs @@ -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(); foreach (var data in user.WarnData.WarnEnds) @@ -50,6 +50,13 @@ public async Task initDatabase() tempUserData.BanData.BanEnd = user.BanData.BanEnd; tempUserData.BanData.IsBanned = user.BanData.IsBanned; + var tempRolesData = new List(); + foreach (var role in user.UserRoles) + { + tempRolesData.Add(role); + } + tempUserData.UserRoles = tempRolesData; + edenorData.Users.Add(tempUserData); } } @@ -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!");*/ } } @@ -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) @@ -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; } @@ -157,6 +173,9 @@ public UserData(ulong userId) [JsonInclude] public UserWarnData WarnData { get; set; } + + [JsonInclude] + public List UserRoles { get; set; } } class UserBanData @@ -213,6 +232,9 @@ class UserDataJson [JsonInclude] public UserWarnDataJson WarnData { get; set; } + + [JsonInclude] + public List UserRoles { get; set; } } class UserBanDataJson