public static async Task <T> Load <T>(string filePath, ulong setId = 0, bool logExceptions = true) where T : MemoryBase { if (!File.Exists(filePath)) { return(null); } try { var jObj = JObject.Parse(await File.ReadAllTextAsync(filePath)); if (jObj != null) { T result = (T)Activator.CreateInstance(typeof(T)); result.id = setId; result.Initialize(); result.ReadFromJson(jObj); return(result); } } catch (Exception e) { if (logExceptions) { await MopBot.HandleException(e, "An error has occured when loading memory."); } } return(null); }
public async Task UpdateSystems(bool allowBreak) { for (int i = 0; i < systems.Count; i++) { var system = systems[i]; try { if (!await system.Update()) { if (allowBreak) { break; } } else if (MopBot.client?.ConnectionState == ConnectionState.Connected) { foreach (var server in MopBot.client.Guilds) { if (system.IsEnabledForServer(server)) { await system.ServerUpdate(server); } } } } catch (Exception e) { await MopBot.HandleException(e); break; } } }
private static async Task ModifyXP(Func <ulong, ulong> xpModifier, SocketGuildUser user, SocketUserMessage message = null) { var server = user.Guild; var serverMemory = MemorySystem.memory[server]; var userMemory = serverMemory[user]; var xpUserData = userMemory.GetData <XPSystem, XPServerUserData>(); uint prevLvl = XPToLevel(xpUserData.xp); xpUserData.xp = xpModifier(xpUserData.xp); uint newLvl = XPToLevel(xpUserData.xp); if (message != null) { if (newLvl > prevLvl) { try { await message.AddReactionAsync(EmoteUtils.Parse("🌟")); } catch (Exception e) { await MopBot.HandleException(e); } } /*var mentionChannel = (ITextChannel)(serverMemory.GetData<ChannelSystem,ChannelServerData>().GetChannelByRole(ChannelRole.BotArea) ?? channel); * string text = $"{user.Name()} has just reached level {newLvl}! :star:"; * * var xpServerData = serverMemory.GetData<XPSystem,XPServerData>(); * * if(xpServerData.levelRewards.TryGetValue(newLvl,out ulong[] roleIds)) { * var oldAccessLevel = user.GetAccessLevel(); * var oldCommandList = CommandService.commands.Where(h => oldAccessLevel>=h.minAccessLevel); * var roles = roleIds.Select(id => server.GetRole(id)); * * text += $"\r\nThe following roles are now available to them:```{string.Join("\r\n",roles.Select(role => role.Name))}```"; * * await user.AddRolesAsync(roles); * * var newAccessLevel = user.GetAccessLevel(roleIds); * var newCommandList = CommandService.commands.Where(h => newAccessLevel>=h.minAccessLevel); * var newCommandsOnly = newCommandList.Where(h => !oldCommandList.Contains(h)).ToArray(); * * if(newCommandsOnly.Length>0) { * text += $"\r\nThe following commands are now available to them:```{string.Join("\r\n",newCommandsOnly.Select(h => $"{string.Join("/",h.aliases)}-{h.description}"))}```"; * } * } * * await mentionChannel.SendMessageAsync(text);*/ } }
public override async Task ServerUpdate(SocketGuild server) { var memory = server.GetMemory().GetData <ConfigurationSystem, ConfigurationServerData>(); string nickname = memory.forcedNickname; var currentUser = server.GetUser(MopBot.client.CurrentUser.Id); if (currentUser != null && !string.Equals(currentUser.Nickname, nickname, StringComparison.InvariantCulture) && !(nickname == "MopBot" && currentUser.Nickname == null) && currentUser.HasDiscordPermission(p => p.ChangeNickname)) { try { await currentUser.ModifyAsync(u => u.Nickname = nickname); } catch (Exception e) { await MopBot.HandleException(e); } } }
public async Task MoveMessagesCommand(SocketTextChannel sourceChannel, int numMessages, SocketTextChannel destinationChannel, ulong bottomMessageId = 0, bool allowGrouping = true) { var context = Context; context.server.CurrentUser.RequirePermission(destinationChannel, DiscordPermission.ManageMessages); var messageList = await CopyMessagesInternal(sourceChannel, numMessages, destinationChannel, bottomMessageId, allowGrouping); try { await context.socketTextChannel.DeleteMessagesAsync(messageList); } catch (Exception e) { await MopBot.HandleException(e); await context.ReplyAsync($"Error deleting messages: ```{string.Join("\r\n", messageList.Select(m => m == null ? "NULL" : m.Id.ToString()))}```"); } }
public async Task MoveMessagesCommand(int numMessages, SocketGuildChannel channel, ulong bottomMessageId = 0) { var context = Context; context.server.CurrentUser.RequirePermission(channel, DiscordPermission.ManageMessages); if (!(channel is ITextChannel toChannel)) { throw new BotError($"<#{channel.Id}> isn't a text channel."); } var messageList = await CopyMessagesInternal(numMessages, toChannel, bottomMessageId); try { await context.socketTextChannel.DeleteMessagesAsync(messageList); } catch (Exception e) { await MopBot.HandleException(e); await context.ReplyAsync($"Error deleting messages: ```{string.Join("\r\n",messageList.Select(m => m==null ? "NULL" : m.Id.ToString()))}```"); } }