public Task <Role> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); var res = ctx.Roles.SingleOrDefault(x => x.NormalizedRoleName.Equals(normalizedRoleName)); return(Task.FromResult(res)); }
public bool UpdateField(AvatarBotContext ctx, string field, string value) { try { switch (field.ToLower()) { case "name": Name = value; return(true); case "alias": Alias = value; return(true); case "description": Description = value; return(true); case "icon": Icon = value; return(true); case "colour": Colour = ParseColour(value); return(true); case "hidden": Hidden = value == "1" || value.ToLower() == "true"; return(true); } } catch (FormatException e) { Console.WriteLine($"Caught an exception on update for {field} with value {value}:\n{e.Message}\n{e.StackTrace}"); throw new Exception($"Couldn't update {field} with value {value}: {e.Message}"); } return(false); }
public async Task ChangeLocalization([Summary("New prefix for the bot's commands")] string local) { using (var ctx = new AvatarBotContext()) { var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { local = local.ToLower(); if (local.Length != 2) { throw new Exception(Localization.Format(server.Localization, "local_too_long", local)); } server.Localization = local; ctx.SaveChanges(); await ReplyAsync(Localization.Format(server.Localization, "local_changed", local)); } catch (Exception e) { await ReplyAsync(e.Message); Console.WriteLine($"Exception: {e.Message}:\n{e.StackTrace}"); } } }
public async Task ChangePrefix([Summary("New prefix for the bot's commands")] char prefix) { using (var ctx = new AvatarBotContext()) { var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { if (prefix == '@' || prefix == '#') { throw new Exception(Localization.Format(server.Localization, "prefix_failed", prefix)); } server.Prefix = $"{prefix}"; ctx.SaveChanges(); await ReplyAsync(Localization.Format(server.Localization, "prefix_changed", prefix)); } catch (Exception e) { await ReplyAsync(e.Message); Console.WriteLine($"Exception: {e.Message}:\n{e.StackTrace}"); } } }
public async Task NPCSay([Remainder] [Summary("By default, only needs the text to display. It will appear as if *A Passerby* had just talked.\n" + "If you want, you can give more details by supplying **key=value** pairs:\n" + "- **colour=xxx**: defines the color of the border.\n" + "- **name=xxx**: changes the name to xxx\n" + "- **icon=\"xxx.jpg\"**: must be a valid URL of an image (the actual image, not a page containing the image) and will display it instead of the clan mon.")] CommandOptions <Character> options) { using (var ctx = new AvatarBotContext()) { var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { var target = new PC(); target.Update(ctx, options.Params); var msg = await Talk(target, options.Text); msg.Server = server; msg.Player = ctx.Users.Single(x => x.DiscordID == (long)Context.User.Id); ctx.Messages.Add(msg); ctx.SaveChanges(); } catch (Exception e) { await ReplyAsync(Localization.Format(server.Localization, "message_send_error", e.Message)); Console.WriteLine($"{e.Message}\n{e.StackTrace}"); } } }
public Task <IList <User> > GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); IList <User> res = ctx.Users.Where(x => x.Claims.Any(y => y.ClaimType.Equals(claim.Type) && y.ClaimValue.Equals(claim.Value))).ToList(); return(Task.FromResult(res)); }
public List <string> PrintListCharacters(string filter, int detailLevel) { List <string> msg = new List <string>(); using (var ctx = new AvatarBotContext()) { var player = ctx.Users.Single(x => x.DiscordID == (long)Context.User.Id); var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); msg.Add(GetListHeader(filter, detailLevel)); List <PC> pcs = ctx.Characters.OfType <PC>().Where(x => x.Server.DiscordID == (long)Context.Guild.Id && (filter.Equals("") || x.Name.Contains(filter) || x.Alias.Contains(filter)) && (x.Hidden == false || ctx.HasPrivilege(server.ID, player.ID))).ToList(); foreach (var c in pcs.OrderBy(x => x.Name)) { if (detailLevel != 0 || c.Player.DiscordID == (long)Context.User.Id) { PC defPC = ctx.GetDefaultCharacter((ulong)c.Server.DiscordID, (ulong)c.Player.DiscordID); bool def = defPC != null && defPC.ID == c.ID; if (def || detailLevel != 1) { msg.Add(def ? $"**{c.Name}** (Alias: {c.Alias})" : $"{c.Name} (Alias: {c.Alias})"); } } } } return(msg); }
public async Task Say([Summary("The text to format, with an optional parameter or $alias")][Remainder] CommandOptions <PC> options) { using (var ctx = new AvatarBotContext()) { var server = ctx.Servers.SingleOrDefault(x => x.DiscordID == (long)Context.Guild.Id); try { if (options.Target == null) { throw new Exception($"target character not found: doesn't exist or you don't have the rights for it?"); } var msg = await Talk(options.Target, options.Text); ctx.Messages.Add(msg); msg.Server = server; msg.Player = ctx.Users.SingleOrDefault(x => x.DiscordID == (long)Context.User.Id); ctx.Entry(msg.Player).State = EntityState.Unchanged; ctx.Entry(msg.Server).State = EntityState.Unchanged; ctx.SaveChanges(); } catch (Exception e) { await ReplyAsync(Localization.Format(server.Localization, "message_send_error", e.Message)); Console.WriteLine($"{e.Message}\n{e.StackTrace}"); } } }
public override void Update(AvatarBotContext ctx, Dictionary <string, string> args) { foreach (var kv in args) { base.UpdateField(ctx, kv.Key, kv.Value); } }
public async Task SetPrivateChannel() { using var ctx = new AvatarBotContext(); var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { var player = ctx.Users.Single(x => x.DiscordID == (long)Context.User.Id); //player.LoadPrivateChannels(ctx); var privateChannel = player.PrivateChannels.SingleOrDefault(x => x.Server.DiscordID == (long)Context.Guild.Id); if (privateChannel == null) { privateChannel = new Model.Servers.PrivateChannel() { Player = player, ChannelDiscordID = (long)Context.Channel.Id, Server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id) } } ; else { privateChannel.ChannelDiscordID = (long)Context.Channel.Id; } ctx.SaveChanges(); await ReplyAsync(Localization.Format(server.Localization, "private_channel_set", Context.Channel.Name)); } catch (Exception e) { await ReplyAsync(Localization.Format(server.Localization, "private_channel_failed", Context.Channel.Name, e.Message)); Console.WriteLine($"Couldn't set #{Context.Channel.Name} as your private channel: {e.Message}\n{e.StackTrace}"); } }
public virtual void Update(AvatarBotContext ctx, Dictionary <string, string> args) { foreach (var kv in args) { UpdateField(ctx, kv.Key, kv.Value); } }
public static List <Embed> GetProfile(this PC pc, ulong serverID, ulong userID, ulong chanID, bool fullDetails = false) { var emds = new List <Embed>(); using var ctx = new AvatarBotContext(); pc = ctx.PCs.Find(pc.ID); // Public data var emd = new EmbedBuilder(); emd.WithTitle(pc.Name); emd.WithColor(new Color((uint)pc.Colour)); emd.WithThumbnailUrl("https://gamepedia.cursecdn.com/l5r_gamepedia_en/thumb/1/1f/Rings.png/300px-Rings.png"); if (pc.Icon != "") { emd.WithThumbnailUrl(pc.Icon); } if (pc.Description != "") { emd.AddField("Description", pc.Description); } emds.Add(emd.Build()); return(emds); }
public Task <User> FindByIdAsync(int userId, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); var user = ctx.Users.SingleOrDefault(x => x.ID == userId); return(Task.FromResult(user)); }
public Task <Role> FindByIdAsync(int roleId, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); var res = ctx.Roles.SingleOrDefault(x => x.ID == roleId); return(Task.FromResult(res)); }
public void LoadDefaultCharacters(AvatarBotContext ctx) { ctx.Entry(this).Collection(x => DefaultCharacters).Query().Load(); foreach (var dc in DefaultCharacters) { ctx.Entry(dc).Reference(x => x.Server).Load(); } }
public Task SetRoleNameAsync(Role role, string roleName, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); ctx.Roles.Attach(role); role.Name = roleName; ctx.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified; ctx.SaveChanges(); return(Task.FromResult(IdentityResult.Success)); }
public Task <User> FindByIdAsync(string userId, CancellationToken cancellationToken) { if (userId == null) { throw new ArgumentNullException(nameof(userId)); } using var ctx = new AvatarBotContext(); var user = ctx.Users.SingleOrDefault(x => x.UserName.Equals(userId)); return(Task.FromResult(user)); }
public Task <Role> FindByIdAsync(string roleId, CancellationToken cancellationToken) { if (roleId == null) { throw new ArgumentNullException(nameof(roleId)); } using var ctx = new AvatarBotContext(); var res = ctx.Roles.SingleOrDefault(x => x.Name.Equals(roleId)); return(Task.FromResult(res)); }
public Task <IList <Claim> > GetClaimsAsync(Role role, CancellationToken cancellationToken = default) { using var ctx = new AvatarBotContext(); ctx.Roles.Attach(role); IList <Claim> res = new List <Claim>(); foreach (var rc in role.Claims) { res.Add(rc.ToClaim()); } return(Task.FromResult(res)); }
public bool IsGM(AvatarBotContext ctx, ulong userID) { if (ctx.Entry(this).State == EntityState.Detached) { ctx.Servers.Attach(this); } var player = ctx.Users.Single(x => x.DiscordID == (long)userID); ctx.Entry(this).Collection(x => x.Roles).Load(); return(Roles.Any(x => x.UserID == player.ID && x.Role.Name.Equals("GM"))); }
public Task RemoveClaimAsync(Role role, Claim claim, CancellationToken cancellationToken = default) { using var ctx = new AvatarBotContext(); ctx.Roles.Attach(role); var existing = role.Claims.SingleOrDefault(x => x.ClaimType == claim.Type && x.ClaimValue == claim.Value); if (existing != null) { role.Claims.Remove(existing); } ctx.SaveChanges(); return(Task.CompletedTask); }
public Task SetUserNameAsync(User user, string userName, CancellationToken cancellationToken) { if (user == null) { throw new ArgumentNullException(nameof(user)); } using var ctx = new AvatarBotContext(); ctx.Users.Attach(user); user.UserName = userName; ctx.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified; ctx.SaveChanges(); return(Task.CompletedTask); }
public Task AddClaimAsync(Role role, Claim claim, CancellationToken cancellationToken = default) { using var ctx = new AvatarBotContext(); ctx.Roles.Attach(role); if (!role.Claims.Any(x => x.ClaimType.Equals(claim.Type) && x.ClaimType.Equals(claim.Value))) { role.Claims.Add(new RoleClaim() { Role = role, ClaimType = claim.Type, ClaimValue = claim.Value }); } ctx.SaveChanges(); return(Task.CompletedTask); }
public async Task Print([Summary("The alias of the character or technique to display.\n\n" + "If its a character you own and you're on your private channel, it will display everything, otherwise only the public profile.")] string nameOrAlias, string detailed = "") { ulong chanID = Context.Channel.Id; ulong serverID = Context.Guild.Id; var emds = new List <Embed>(); using var ctx = new AvatarBotContext(); try { var player = ctx.Users.Single(x => x.DiscordID == (long)Context.User.Id); var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); var c = ctx.Characters.SingleOrDefault(x => x.Server.DiscordID == (long)serverID && (x.Name.Equals(nameOrAlias) || x.Alias.Equals(nameOrAlias)) && (x.Hidden == false || ctx.HasPrivilege(server.ID, player.ID))); if (c != null) { if (c is PC pc) { emds.AddRange(pc.GetProfile(Context.Guild.Id, Context.User.Id, Context.Channel.Id, detailed.Equals("full", StringComparison.OrdinalIgnoreCase))); } else if (c is NPC npc) { emds.AddRange(npc.GetProfile(Context.Guild.Id, Context.User.Id, Context.Channel.Id, detailed.Equals("full", StringComparison.OrdinalIgnoreCase))); } } else { await ReplyAsync(Localization.Format(server.Localization, "characters_header", nameOrAlias)); } foreach (var e in emds) { await ReplyAsync("", false, e); } await Context.Channel.DeleteMessageAsync(Context.Message); } catch (Exception e) { Console.WriteLine($"Exception: {e.Message}: {e.StackTrace}"); var inner = e.InnerException; while (inner != null) { Console.WriteLine($"Inner Exception:\n{inner.Message}: {inner.StackTrace}"); inner = inner.InnerException; } } }
public Task RemoveClaimsAsync(User user, IEnumerable <Claim> claims, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); ctx.Users.Attach(user); foreach (var c in claims) { var existing = user.Claims.SingleOrDefault(x => x.ClaimType == c.Type && x.ClaimValue == c.Value); if (existing != null) { user.Claims.Remove(existing); } } ctx.SaveChanges(); return(Task.CompletedTask); }
public Task SetNormalizedUserNameAsync(User user, string normalizedName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) { throw new ArgumentNullException(nameof(user)); } using var ctx = new AvatarBotContext(); ctx.Users.Attach(user); user.NormalizedUserName = normalizedName ?? throw new ArgumentNullException(nameof(normalizedName)); ctx.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified; ctx.SaveChanges(); return(Task.FromResult <object>(null)); }
public async Task PrintList([Summary("The first required parameter is the type of things you want to display. " + "It must be one of MyCharacters, Characters, AllCharacters, Skills or Techniques.")] string type, [Remainder] [Summary("You can filter those results by adding something after that, for example: " + "**!list techniques soul**.\n\n" + "Note that in the case of techniques, the filter is **required** (too many things to display at once otherwise).")] string filter = "") { filter = filter.ToLower().Trim(); using var ctx = new AvatarBotContext(); var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { List <string> msg; type = type.ToLower(); switch (type) { case "mycharacters": msg = PrintListCharacters(filter, 0); break; case "characters": msg = PrintListCharacters(filter, 1); break; case "allcharacters": msg = PrintListCharacters(filter, 2); break; default: throw new System.Exception(Localization.Format(server.Localization, "unknown_command", type)); } if (msg.Count == 0) { await ReplyAsync(Localization.Format(server.Localization, "no_character_found")); } else { foreach (var m in msg.FitDiscordMessageSize()) { await ReplyAsync(m); } } await Context.Channel.DeleteMessageAsync(Context.Message); } catch (Exception e) { await ReplyAsync(Localization.Format(server.Localization, "list_failed", e.Message)); Console.WriteLine($"Exception: {e.Message}:\n{e.StackTrace}"); } }
public Task ReplaceClaimAsync(User user, Claim claim, Claim newClaim, CancellationToken cancellationToken) { using var ctx = new AvatarBotContext(); ctx.Users.Attach(user); var existing = user.Claims.SingleOrDefault(x => x.ClaimType == claim.Type && x.ClaimValue == claim.Value); if (existing != null) { existing.ClaimType = newClaim.Type; existing.ClaimValue = newClaim.Value; } ctx.SaveChanges(); return(Task.CompletedTask); }
public async Task CreateCharacter([Summary("Alias of the character to create")] string alias, [Summary("key=value pairs containing more informations about the character.\n" + "If a value contains more than one word, put it between \"\".\n" + "To learn more about the available fields, use the command **character fields**.")] [Remainder] CommandOptions <Character> options) { using var ctx = new AvatarBotContext(); using var dbTransaction = ctx.Database.BeginTransaction(); var server = ctx.Servers.Single(x => x.DiscordID == (long)Context.Guild.Id); try { var player = ctx.Users.Single(x => x.DiscordID == (long)Context.User.Id); var newChar = new PC() { Alias = alias, Name = alias, Server = server, Player = player }; ctx.Characters.Add(newChar); // Users can't have hidden characters if (!ctx.HasPrivilege(server.ID, player.ID)) { options.Params["hidden"] = "false"; } var msg = UpdateChar(ctx, newChar, options.Params); if (msg == "") { msg = Localization.Format(server.Localization, "character_created", newChar.Name); } ctx.SaveChanges(); dbTransaction.Commit(); await ReplyAsync(msg); await Context.Channel.DeleteMessageAsync(Context.Message); } catch (Exception e) { dbTransaction.Rollback(); await ReplyAsync(Localization.Format(server.Localization, "character_creation_failed", e.Message)); Console.WriteLine($"{e.Message}\n{e.StackTrace}"); } }
public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) { try { using var ctx = new AvatarBotContext(); var pc = new PrivateCharacter() { Character = ctx.GetPlayerCharacter(context.Guild.Id, context.User.Id, input.Replace("$", "")) }; Console.WriteLine($"I found a matching private character: {pc.Character.Name}"); return(Task.FromResult(TypeReaderResult.FromSuccess(pc))); } catch (Exception) { return(Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Could not find the private character."))); } }