Response placeholders work. will do trigger placeholders tomorrow

This commit is contained in:
Kwoth
2016-10-09 06:38:46 +02:00
parent 0bc377b0c2
commit ae998c36b3
6 changed files with 830 additions and 6 deletions

View File

@@ -13,7 +13,7 @@ using Discord;
using NadekoBot.Extensions;
namespace NadekoBot.Modules.CustomReactions
{
{
[NadekoModule("CustomReactions",".")]
public class CustomReactions : DiscordModule
{
@@ -46,17 +46,17 @@ namespace NadekoBot.Modules.CustomReactions
GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
if (reactions != null && reactions.Any())
{
var reaction = reactions.Where(cr => cr.Trigger == umsg.Content).Shuffle().FirstOrDefault();
var reaction = reactions.Where(cr => cr.TriggerWithContext(umsg) == umsg.Content).Shuffle().FirstOrDefault();
if (reaction != null)
{
try { await channel.SendMessageAsync(reaction.Response).ConfigureAwait(false); } catch { }
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
return;
}
}
var greaction = GlobalReactions.Where(cr => cr.Trigger == umsg.Content).Shuffle().FirstOrDefault();
var greaction = GlobalReactions.Where(cr => cr.TriggerWithContext(umsg) == umsg.Content).Shuffle().FirstOrDefault();
if (greaction != null)
{
try { await channel.SendMessageAsync(greaction.Response).ConfigureAwait(false); } catch { }
try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
return;
}
});

View File

@@ -0,0 +1,38 @@
using Discord;
using NadekoBot.Extensions;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.CustomReactions
{
public static class Extensions
{
public static Dictionary<string, Func<IUserMessage, string>> placeholders = new Dictionary<string, Func<IUserMessage, string>>()
{
{"%mention%", (ctx) => { return $"<@!{NadekoBot.Client.GetCurrentUser().Id}>"; } },
{"%user%", (ctx) => { return ctx.Author.Mention; } },
{"%target%", (ctx) => { return ctx.MentionedUsers.Shuffle().FirstOrDefault()?.Mention ?? "Nobody"; } },
{"%rng%", (ctx) => { return new NadekoRandom().Next(0,10).ToString(); } }
};
private static string ResolveCRString(this string str, IUserMessage ctx)
{
foreach (var ph in placeholders)
{
str = str.Replace(ph.Key, ph.Value(ctx));
}
return str;
}
public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx)
=> cr.Trigger.ResolveCRString(ctx);
public static string ResponseWithContext(this CustomReaction cr, IUserMessage ctx)
=> cr.Response.ResolveCRString(ctx);
}
}