public static string?Get(this SnowflakeObject id, string element, string?defaultVal) { return(id switch { DiscordChannel _ => Get(id.Id.ToString(), element, Channel, defaultVal), DiscordGuild _ => Get(id.Id.ToString(), element, Guild, defaultVal), _ => throw new ArgumentException("Invalid Snowflake! (Supports Guilds and Channels)") });
private async Task ConfigCmd(CommandContext ctx, SnowflakeObject target) { if (ctx.Channel.Get(ConfigManager.Enabled).True()) { await ctx.TriggerTypingAsync(); await ctx.RespondAsync(target.GetStr(CommandArr.GetCommandNames())); } }
public GuildEntity GetOrCreate(SnowflakeObject sf) { if (!(sf is DiscordGuild gld)) { throw new InvalidOperationException("Given entity was not a DiscordGuild."); } if (CachedEntities.TryGetValue(gld.Id, out var entity)) { _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://guild/{gld.Id}"); return(entity); } entity = Get(gld.Id); if (!(entity is null)) { if (!CachedEntities.TryAdd(gld.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); } Create(new GuildEntity { Id = gld.Id, Prefixes = new List <string> { _db.Configuration.Prefix }, Levenshtein = true, Music = new MusicConfiguration { Volume = 50, DjOnly = false, Loop = false }, CustomCommands = new List <CustomCommand>() }); entity = Get(gld.Id); if (!CachedEntities.TryAdd(gld.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); }
private async Task ConfigCmd(CommandContext ctx, SnowflakeObject target, string element, string value, bool noComment = false) { if (ctx.Channel.Get(ConfigManager.Enabled).True() || ctx.Guild.Channels.All(s => s.Value.Get(ConfigManager.Enabled).False())) { if (!noComment) { await ctx.TriggerTypingAsync(); } if (string.Equals(element, ConfigManager.Prefix, StringComparison.CurrentCultureIgnoreCase)) { target.Set(ConfigManager.Prefix, value); } else if (CommandArr.GetGroupNames().Contains(element)) { foreach (string t in CommandArr.GetCommandNames(element)) { await ConfigCmd(ctx, target, t, value, true); } } else if (!CommandArr.GetCommandNames().Contains(element)) { throw new ArgumentException($"Element ({element}) not in CommandArr"); } else { target.Set(element, (await ctx.Client.GetCommandsNext().ConvertArgument <bool>(value, ctx)).ToString()); } if (target is DiscordGuild guild) { if (string.Equals(element, ConfigManager.Enabled, StringComparison.CurrentCultureIgnoreCase)) { await ctx.RespondAsync("Please do not EVER set \"enabled\" to false globally!"); return; } foreach ((ulong _, DiscordChannel channel) in guild.Channels) { await ConfigCmd(ctx, channel, element, value, true); } } if (!noComment) { await ctx.RespondAsync($"Set {element} to {value}"); } } }
public UserEntity GetOrCreate(SnowflakeObject sf) { if (!(sf is DiscordUser usr)) { throw new InvalidOperationException("Given entity was not a DiscordUser."); } if (CachedEntities.TryGetValue(usr.Id, out var entity)) { _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://user/{usr.Id}"); return(entity); } entity = Get(usr.Id); if (!(entity is null)) { if (!CachedEntities.TryAdd(usr.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); } Create(new UserEntity { Id = usr.Id, RewardCooldown = DateTime.Now, Gold = 0, IsBlacklisted = false, Experience = 0, Playlists = new List <UserPlaylist>() }); entity = Get(usr.Id); if (!CachedEntities.TryAdd(usr.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); }
public MemberEntity GetOrCreate(SnowflakeObject sf, ulong guildId) { if (!(sf is DiscordMember mbr)) { throw new InvalidOperationException("Given entity was not a DiscordMember."); } if (CachedEntities.TryGetValue(mbr.Id, out var entity)) { _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://member/{mbr.Id}"); return(entity); } entity = Get(mbr.Id); if (!(entity is null)) { if (!CachedEntities.TryAdd(mbr.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); } Create(new MemberEntity { Id = mbr.Id, RewardCooldown = DateTime.Now, Experience = 0, GuildId = guildId }); entity = Get(mbr.Id); if (!CachedEntities.TryAdd(mbr.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); }
public ChannelEntity GetOrCreate(SnowflakeObject sf) { if (!(sf is DiscordChannel chn)) { throw new InvalidOperationException("Given entity was not a DiscordChannel."); } if (CachedEntities.TryGetValue(chn.Id, out var entity)) { _db.Logger.Print(LogLevel.Debug, "Database", $"GET_FROM_CACHE://channel/{chn.Id}"); return(entity); } entity = Get(chn.Id); if (!(entity is null)) { if (!CachedEntities.TryAdd(chn.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); } Create(new ChannelEntity { Id = chn.Id, Modules = new ConcurrentDictionary <string, ModuleConfiguration>(), RiotRegion = "EUW" }); entity = Get(chn.Id); if (!CachedEntities.TryAdd(chn.Id, entity)) { throw new InvalidOperationException("The object was already present in cache but was not pulled earlier. This should not happen."); } return(entity); }
private async Task ConfigCmd(CommandContext ctx, SnowflakeObject target, string element) { if (ctx.Channel.Get(ConfigManager.Enabled).True()) { await ctx.TriggerTypingAsync(); if (string.Equals(element, ConfigManager.Prefix, StringComparison.CurrentCultureIgnoreCase)) { await ctx.RespondAsync( $"{element}: {target.Get(element, target is DiscordGuild ? Common.Prefix : ((DiscordChannel) target).Guild.Get(ConfigManager.Prefix, Common.Prefix))}"); } else { if (!CommandArr.GetCommandNames().Contains(element)) { throw new ArgumentException($"Element ({element}) not in CommandArr"); } await ctx.RespondAsync($"{element}: {target.Get(element)}"); } } }
private static async Task TTT(CommandContext ctx, SnowflakeObject member) { var isAuthorTurn = false; var moves = new List <int>(); InteractivityExtension interactivity = ctx.Client.GetInteractivity(); DiscordEmoji placeholder = DiscordEmoji.FromName(ctx.Client, ":white_large_square:"); DiscordEmoji circle = DiscordEmoji.FromName(ctx.Client, ":o:"); DiscordEmoji cross = DiscordEmoji.FromName(ctx.Client, ":x:"); DiscordMessage tic = await ctx.Channel.SendMessageAsync( placeholder + placeholder + placeholder + "\n" + placeholder + placeholder + placeholder + "\n" + placeholder + placeholder + placeholder ); var arr = new object[tic.Content.Length]; var i = 0; foreach (char chr in tic.Content) { arr.SetValue(chr, i); i++; } await Inputs(); async Task Inputs() { InteractivityResult <DiscordMessage> response = await interactivity.WaitForMessageAsync( x => x.Channel == ctx.Message.Channel && (x.Author.Id == ctx.Member.Id || x.Author.Id == member.Id) && x.Content.Length < 2 && int.TryParse(x.Content, out int nr) && nr != 0, TimeSpan.FromSeconds(30)); int.TryParse(response.Result.Content, out int n); n--; if (n > 2) { n++; } if (n > 6) { n++; // skip newline } if (moves.Contains(n)) { await MoveTaken(); } else if (response.Result.Author.Id == member.Id && isAuthorTurn == false) // Tagged { moves.Add(n); await Blue(); await response.Result.DeleteAsync(); isAuthorTurn = true; } else if (response.Result.Author.Id == ctx.Member.Id && isAuthorTurn) // Author { moves.Add(n); await Green(); await response.Result.DeleteAsync(); isAuthorTurn = false; } else { await ctx.RespondAsync("It's not your turn!"); } await Inputs(); async Task Blue() { arr[n] = circle; string newMsg = arr.Aggregate("", (str, obj) => str + obj); await tic.ModifyAsync(newMsg); } async Task Green() { arr[n] = cross; string newMsg = arr.Aggregate("", (str, obj) => str + obj); await tic.ModifyAsync(newMsg); } async Task MoveTaken() { DiscordMessage moveTakenMsg = await ctx.RespondAsync("This move has already been done! " + response.Result.Author.Mention); await Task.Delay(5000); await response.Result.DeleteAsync("Move Taken | TTT"); await moveTakenMsg.DeleteAsync($"Move taken | TicTacToe move by {response.Result.Author.Username}"); } } }
public static string GetEncryptedID(this SnowflakeObject snowflake) { string idString = snowflake.Id.ToString(); return(EncryptString(idString, Bot.Instance.Config.EncryptionKey)); }
public MemberEntity GetOrCreate(SnowflakeObject sf, DiscordGuild guild) { return(GetOrCreate(sf, guild.Id)); }
public static bool?Get(this SnowflakeObject id, string element, bool?defaultVal = true) => GenericExtensions.ParseBool(id.Get(element, defaultVal.GetString()));