public async Task Enlarge(CommandContext ctx, DiscordEmoji emoji) { // _ = emoji.Id == 0: // ? await ctx.RespondAsync($"I can't enlarge unicode emojis, {ctx.User.Username}!") // : await ctx.RespondAsync(embed: new DiscordEmbedBuilder() // .WithDescription($"Emoji Name: {emoji.GetDiscordName()}") // .WithImageUrl(emoji.Url) // .WithColor(new DiscordColor("42d4f5")) // .AddFooter(ctx)); DiscordMessageBuilder builder = new(); if (emoji.Id is 0) { builder.WithContent("I can't enlarge unicode emojis, yet.").WithReply(ctx.Message.Id); await ctx.RespondAsync(builder); } else { DiscordEmbed embed = new DiscordEmbedBuilder() .WithDescription($"Emoji Name: {emoji.GetDiscordName()}") .WithImageUrl(emoji.Url) .WithColor(new DiscordColor("42d4f5")); builder.WithEmbed(embed).WithReply(ctx.Message.Id, true); await builder.SendAsync(ctx.Channel); } }
public async Task Add(CommandContext ctx, [Description("The emoji to bind")] DiscordEmoji emoji, [Description("The role to bind")] DiscordRole role) { if (ctx.Channel.Get(ConfigManager.Enabled) .And(ctx.Channel.GetMethodEnabled())) { await ctx.TriggerTypingAsync(); Dictionary <string, ulong> roles = ctx.Guild.GetReactionRoles(); if (roles.Count > 19) { await ctx.RespondAsync("Too many roles!"); return; } if (roles.ContainsKey(emoji.Name) || roles.ContainsValue(role.Id)) { await ctx.RespondAsync("Element already registered"); return; } Console.WriteLine(role.Name); //I know hard-coding @everyone is bad code but it works for now if (emoji.IsManaged || role.IsManaged || role.Name == "@everyone") { await ctx.RespondAsync("Invalid value"); return; } roles.Add(emoji.GetDiscordName(), role.Id); ctx.Guild.SetReactionRoles(roles); await ctx.RespondAsync("Done!"); } }
private async Task Register(CommandArgs <RoleBotConfig> args) { string emojiName = args["emojiName"]; DiscordEmoji emoji = DiscordEmoji.FromName(Instance.Client, emojiName); emojiName = emoji.GetDiscordName(); if (emojiName != null) { if (!args.Config.EmojiRoles.TryGetValue(emojiName, out ulong roleID)) { roleID = ulong.Parse(args["roleID"]); args.Config.EmojiRoles.Add(emojiName, roleID); DiscordRole role = args.Guild.GetRole(roleID); await args.Channel.SendMessageAsync($"{emojiName} has been mapped to *{role.Name}*."); } else { DiscordRole role = args.Guild.GetRole(roleID); await args.Channel.SendMessageAsync($"{emojiName} is already mapped to *{role?.Name ?? "a role that no longer exists."}*"); } } else { await args.Channel.SendMessageAsync($"The emoji does not exist."); } }
public static int GetAsNumber(this DiscordEmoji emoji) { var emojiName = emoji.GetDiscordName(); var emojiUnicode = EmojiNameToUnicode[emojiName]; var emojiIndex = Array.IndexOf(NumberEmojiUnicodes, emojiUnicode); return(emojiIndex + 1); }
public static string FromDiscord(DiscordEmoji emoji) { if (emoji.Id != default) { return(emoji.Id.ToString()); } return(emoji.GetDiscordName()); }
public static string FormatName(this DiscordEmoji emoji) { if (emoji.RequiresColons) { return($"<{emoji.GetDiscordName()}{emoji.Id}>"); } else { return(emoji.Name); } }
public async Task AddAsync(CommandContext ctx, [Description("desc-emoji")] DiscordEmoji emoji, [Description("desc-role-grant")] DiscordRole role) { if (emoji is DiscordGuildEmoji && !ctx.Guild.Emojis.Select(kvp => kvp.Value).Contains(emoji)) { throw new CommandFailedException(ctx, "cmd-err-rr-emoji-404"); } ReactionRole?rr = await this.Service.GetAsync(ctx.Guild.Id, emoji.GetDiscordName()); if (rr is { })
public async Task DeleteAsync(CommandContext ctx, [Description("Emoji to remove reactions for.")] DiscordEmoji emoji) { if (!this.Shared.EmojiReactions.ContainsKey(ctx.Guild.Id)) { throw new CommandFailedException("This guild has no emoji reactions registered."); } string ename = emoji.GetDiscordName(); if (this.Shared.EmojiReactions[ctx.Guild.Id].RemoveWhere(er => er.Response == ename) == 0) { throw new CommandFailedException("No such reactions found!"); } var eb = new StringBuilder(); try { await this.Database.RemoveAllTriggersForEmojiReactionAsync(ctx.Guild.Id, ename); } catch (Exception e) { this.Shared.LogProvider.LogException(LogLevel.Warning, e); eb.AppendLine($"Warning: Failed to remove reaction from the database."); } DiscordChannel logchn = this.Shared.GetLogChannelForGuild((DiscordClientImpl)ctx.Client, ctx.Guild); if (logchn != null) { var emb = new DiscordEmbedBuilder() { Title = "Several emoji reactions have been deleted", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Removed reaction for emoji", emoji, inline: true); if (eb.Length > 0) { emb.AddField("With errors", eb.ToString()); } await logchn.SendMessageAsync(embed : emb.Build()); } if (eb.Length > 0) { await this.InformFailureAsync(ctx, $"Action finished with following warnings:\n\n{eb.ToString()}"); } else { await this.InformAsync(ctx, $"Removed reactions that contain emoji: {emoji}", important : false); } }
public async Task Remove(CommandContext ctx, [Description("The emoji to unbind")] DiscordEmoji emoji) { if (ctx.Channel.Get(ConfigManager.Enabled) .And(ctx.Channel.GetMethodEnabled())) { await ctx.TriggerTypingAsync(); Dictionary <string, ulong> roles = ctx.Guild.GetReactionRoles(); roles.Remove(emoji.GetDiscordName()); ctx.Guild.SetReactionRoles(roles); await ctx.RespondAsync("Done!"); } }
/// <summary> /// Get the string implementation of an emoji, regardless of its origin /// </summary> /// <param name="emoji">emoji object</param> /// <returns>coded string</returns> public static string GetCode(DiscordEmoji emoji) { if (emoji.RequireColons) { //if the emoji is custom return("<" + emoji.GetDiscordName() + emoji.Id + ">"); } else { //if the emoji is native (Unicode) return(emoji.Name); } }
/// <summary> /// Stores a reply reaction /// </summary> /// <param name="ctx"></param> /// <param name="message"></param> /// <returns></returns> public async Task StoreReactionAsync(CommandContext ctx, DiscordEmoji reaction) { //Store the redis object string key = Namespace.Combine(ctx.Guild, "replies", ctx.Message); await Redis.StoreObjectAsync(key, new Reply() { CommandMsg = ctx.Message.Id, ResponseEmote = reaction.GetDiscordName(), ResponseType = Reply.SnowflakeType.Reaction }); await Redis.SetExpiryAsync(key, ReplyTimeout); }
public async Task <bool> DoActionAsync(DiscordMessage message, DiscordEmoji clickedEmoji, int totalItemsCount) { switch (clickedEmoji.GetDiscordName()) { case RightEmojiName: return(await GoToNextPageAsync(message, totalItemsCount)); case LeftEmojiName: return(await GoToPreviousPageAsync(message)); case FirstEmojiName: return(await GoToFirstPageAsync(message)); case LastEmojiName: return(await GoToLastPageAsync(message, totalItemsCount)); } return(false); }
public async Task <Player> GetWinner() { int mostCount = int.MinValue; DiscordEmoji min = null; foreach (var v in messages) { foreach (var v2 in v.Reactions) { await v.DeleteOwnReactionAsync(v2.Emoji); } foreach (var v2 in v.Reactions) { if (v2.Count > mostCount) { mostCount = v2.Count; min = v2.Emoji; } } } return(GameManager.Players.Where(x => x.DiscordEmoji.GetDiscordName() == min.GetDiscordName()).Single()); }
private async Task InternalStarboardAsync(CommandContext ctx, bool enable, DiscordChannel channel, DiscordEmoji?emoji = null, int?sens = null) { if (enable) { await this.Service.ModifySettingsAsync(ctx.Guild.Id, channel.Id, emoji?.GetDiscordName(), sens); } else { await this.Service.ModifySettingsAsync(ctx.Guild.Id, null); } await ctx.GuildLogAsync(emb => { emb.WithLocalizedTitle("evt-sb-upd"); emb.WithColor(this.ModuleColor); emb.AddLocalizedField("str-status", emoji is null ? "str-disabled" : "str-enabled", inline: true); emb.AddLocalizedTitleField("str-chn", channel.Mention, inline: true, unknown: false); emb.AddLocalizedTitleField("str-star", emoji ?? Emojis.Star, inline: true, unknown: false); emb.AddLocalizedTitleField("str-sensitivity", this.Service.GetStarboardSensitivity(ctx.Guild.Id), inline: true); }); await this.ExecuteGroupAsync(ctx); }
public async Task DeleteAsync(CommandContext ctx, [Description("Emoji to remove reactions for")] DiscordEmoji emoji) { if (!this.Shared.EmojiReactions.TryGetValue(ctx.Guild.Id, out var ereactions)) { throw new CommandFailedException("The emoji reactions are not registered"); } string ename = emoji.GetDiscordName(); if (ereactions.RemoveWhere(er => er.Response == ename) == 0) { throw new CommandFailedException("No such reactions were found!"); } using (var dc = this.Database.CreateContext()) { dc.EmojiReactions.RemoveRange(dc.EmojiReactions.Where(er => er.GuildId == ctx.Guild.Id && er.Reaction == ename)); await dc.SaveChangesAsync(); } var logch = this.Shared.GetLogChannelForGuild(ctx.Client, ctx.Guild); if (!(logch is null)) { var emb = new DiscordEmbedBuilder { Title = "Several emoji reactions have been deleted", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Removed reaction for emoji", emoji, inline: true); await logch.SendMessageAsync(embed : emb.Build()); } await this.InformAsync(ctx, $"Removed reactions that contain emoji: {emoji}", important : false); }
public void SetEmoji(DiscordEmoji value) => Emoji = value.GetDiscordName();
public async Task Enlarge(CommandContext ctx, DiscordEmoji emoji) => await ctx.RespondAsync(embed : new DiscordEmbedBuilder() .WithDescription("Emoji Name: " + emoji.GetDiscordName()) .WithImageUrl(emoji.Url) .WithColor(new DiscordColor("42d4f5")) .AddFooter(ctx));
// Reaction Role was created internal static Task Role_Created(DiscordRole role, DiscordEmoji emoji) { Bot.Client.DebugLogger.LogMessage(LogLevel.Info, "RoleBot", $"{role.Name} is being watched through the emoji {emoji.GetDiscordName()}", DateTime.Now); return(Task.CompletedTask); }
public static int GetReactionsCount(this DiscordMessage msg, DiscordEmoji emoji) { string emojiName = emoji.GetDiscordName(); return(msg.Reactions.FirstOrDefault(r => r.Emoji.GetDiscordName() == emojiName)?.Count ?? 0); }
public static Task <int> RemoveEmojiReactionsEAsync(this ReactionsService service, ulong gid, DiscordEmoji emoji) => service.RemoveEmojiReactionsAsync(gid, emoji.GetDiscordName());
public static Task <int> AddEmojiReactionEAsync(this ReactionsService service, ulong gid, DiscordEmoji emoji, IEnumerable <string> triggers, bool regex) => service.AddEmojiReactionAsync(gid, emoji.GetDiscordName(), triggers, regex);
private async Task AddEmojiReactionAsync(CommandContext ctx, DiscordEmoji emoji, bool regex, params string[] triggers) { if (triggers == null) { throw new InvalidCommandUsageException("Missing trigger words!"); } var eb = new StringBuilder(); foreach (string trigger in triggers.Select(t => t.ToLowerInvariant())) { if (trigger.Length > 120) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} is too long (120 chars max)."); continue; } if (!this.Shared.EmojiReactions.ContainsKey(ctx.Guild.Id)) { if (!this.Shared.EmojiReactions.TryAdd(ctx.Guild.Id, new ConcurrentHashSet <EmojiReaction>())) { throw new ConcurrentOperationException("Failed to create emoji reaction data structure"); } } if (regex && !trigger.IsValidRegex()) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} is not a valid regular expression."); continue; } string ename = emoji.GetDiscordName(); if (this.Shared.EmojiReactions[ctx.Guild.Id].Where(er => er.ContainsTriggerPattern(trigger)).Any(er => er.Response == ename)) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} already exists for this emoji."); continue; } int id = 0; try { id = await this.Database.AddEmojiReactionAsync(ctx.Guild.Id, trigger, ename, regex : regex); } catch (Exception e) { this.Shared.LogProvider.LogException(LogLevel.Warning, e); eb.AppendLine($"Warning: Failed to add trigger {Formatter.Bold(trigger)} to the database."); } EmojiReaction reaction = this.Shared.EmojiReactions[ctx.Guild.Id].FirstOrDefault(tr => tr.Response == ename); if (reaction != null) { if (!reaction.AddTrigger(trigger, isRegex: regex)) { throw new CommandFailedException($"Failed to add trigger {Formatter.Bold(trigger)}."); } } else { if (!this.Shared.EmojiReactions[ctx.Guild.Id].Add(new EmojiReaction(id, trigger, ename, isRegex: regex))) { throw new CommandFailedException($"Failed to add trigger {Formatter.Bold(trigger)}."); } } } DiscordChannel logchn = this.Shared.GetLogChannelForGuild((DiscordClientImpl)ctx.Client, ctx.Guild); if (logchn != null) { var emb = new DiscordEmbedBuilder() { Title = "New emoji reactions added", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Reaction", emoji, inline: true); emb.AddField("Triggers", string.Join("\n", triggers)); if (eb.Length > 0) { emb.AddField("With errors", eb.ToString()); } await logchn.SendMessageAsync(embed : emb.Build()); } if (eb.Length > 0) { await this.InformFailureAsync(ctx, $"Action finished with following warnings/errors:\n\n{eb.ToString()}"); } else { await this.InformAsync(ctx, "Successfully added all given emoji reactions.", important : false); } }
public static bool IsCancelEmoji(this DiscordEmoji emoji) { return(emoji.GetDiscordName() == ":x:"); }
private async Task AddEmojiReactionAsync(CommandContext ctx, DiscordEmoji emoji, bool regex, params string[] triggers) { if (emoji is DiscordGuildEmoji && !ctx.Guild.Emojis.Values.Contains(emoji)) { throw new CommandFailedException("The reaction has to be an emoji from this guild."); } if (triggers is null || !triggers.Any()) { throw new InvalidCommandUsageException("Missing trigger words!"); } if (!this.Shared.EmojiReactions.ContainsKey(ctx.Guild.Id)) { if (!this.Shared.EmojiReactions.TryAdd(ctx.Guild.Id, new ConcurrentHashSet <EmojiReaction>())) { throw new ConcurrentOperationException("Failed to create emoji reaction data structure"); } } int id; using (DatabaseContext db = this.Database.CreateContext()) { DatabaseEmojiReaction dber = db.EmojiReactions.FirstOrDefault(er => er.GuildId == ctx.Guild.Id && er.Reaction == emoji.GetDiscordName()); if (dber is null) { dber = new DatabaseEmojiReaction { GuildId = ctx.Guild.Id, Reaction = emoji.GetDiscordName() }; db.EmojiReactions.Add(dber); await db.SaveChangesAsync(); } foreach (string trigger in triggers) { dber.DbTriggers.Add(new DatabaseEmojiReactionTrigger { ReactionId = dber.Id, Trigger = regex ? trigger : Regex.Escape(trigger) }); } await db.SaveChangesAsync(); id = dber.Id; } var eb = new StringBuilder(); foreach (string trigger in triggers.Select(t => t.ToLowerInvariant())) { if (trigger.Length > 120) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} is too long (120 chars max)."); continue; } if (regex && !trigger.IsValidRegex()) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} is not a valid regular expression."); continue; } ConcurrentHashSet <EmojiReaction> ereactions = this.Shared.EmojiReactions[ctx.Guild.Id]; string ename = emoji.GetDiscordName(); if (ereactions.Where(er => er.ContainsTriggerPattern(trigger)).Any(er => er.Response == ename)) { eb.AppendLine($"Error: Trigger {Formatter.Bold(trigger)} already exists for this emoji."); continue; } EmojiReaction reaction = ereactions.FirstOrDefault(tr => tr.Response == ename); if (reaction is null) { if (!ereactions.Add(new EmojiReaction(id, trigger, ename, isRegex: regex))) { throw new CommandFailedException($"Failed to add trigger {Formatter.Bold(trigger)}."); } } else { if (!reaction.AddTrigger(trigger, isRegex: regex)) { throw new CommandFailedException($"Failed to add trigger {Formatter.Bold(trigger)}."); } } } DiscordChannel logchn = this.Shared.GetLogChannelForGuild(ctx.Client, ctx.Guild); if (!(logchn is null)) { var emb = new DiscordEmbedBuilder { Title = "New emoji reactions added", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Reaction", emoji, inline: true); emb.AddField("Triggers", string.Join("\n", triggers)); if (eb.Length > 0) { emb.AddField("With errors", eb.ToString()); } await logchn.SendMessageAsync(embed : emb.Build()); } if (eb.Length > 0) { await this.InformFailureAsync(ctx, $"Action finished with following warnings/errors:\n\n{eb.ToString()}"); } else { await this.InformAsync(ctx, "Successfully added all given emoji reactions.", important : false); } }
public static bool IsNumberEmoji(this DiscordEmoji emoji) { return(EmojiNameToUnicode.ContainsKey(emoji.GetDiscordName())); }