.greetmsg, .greetdmmsg, .acr and .byemsg now accept json which will cause the output to be a discord embed. You can create a proper json using http://nadekobot.xyz/embedbuilder/

This commit is contained in:
Kwoth 2017-02-06 20:09:47 +01:00
parent 4429687bd5
commit 91efe4db48
3 changed files with 229 additions and 47 deletions

View File

@ -0,0 +1,91 @@
using Discord;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.DataStructures
{
public class CREmbed
{
private static readonly Logger _log;
public string PlainText { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public CREmbedFooter Footer { get; set; }
public string Thumbnail { get; set; }
public string Image { get; set; }
public CREmbedField[] Fields { get; set; }
public uint Color { get; set; } = 7458112;
static CREmbed()
{
_log = LogManager.GetCurrentClassLogger();
}
public bool IsValid =>
!string.IsNullOrWhiteSpace(Title) ||
!string.IsNullOrWhiteSpace(Description) ||
!string.IsNullOrWhiteSpace(Thumbnail) ||
!string.IsNullOrWhiteSpace(Image) ||
(Footer != null && (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl))) ||
(Fields != null && Fields.Length > 0);
public EmbedBuilder ToEmbed()
{
var embed = new EmbedBuilder()
.WithTitle(Title)
.WithDescription(Description)
.WithColor(new Discord.Color(Color));
if (Footer != null)
embed.WithFooter(efb => efb.WithIconUrl(Footer.IconUrl).WithText(Footer.Text));
embed.WithThumbnailUrl(Thumbnail)
.WithImageUrl(Image);
if (Fields != null)
foreach (var f in Fields)
{
embed.AddField(efb => efb.WithName(f.Name).WithValue(f.Value).WithIsInline(f.Inline));
}
return embed;
}
public static bool TryParse(string input, out CREmbed embed)
{
embed = null;
if (string.IsNullOrWhiteSpace(input))
return false;
try
{
var crembed = JsonConvert.DeserializeObject<CREmbed>(input);
if (!crembed.IsValid)
return false;
embed = crembed;
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public class CREmbedField
{
public string Name { get; set; }
public string Value { get; set; }
public bool Inline { get; set; }
}
public class CREmbedFooter {
public string Text { get; set; }
public string IconUrl { get; set; }
}
}

View File

@ -2,6 +2,7 @@
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.DataStructures;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
@ -83,77 +84,137 @@ namespace NadekoBot.Modules.Administration
return settings; return settings;
} }
//todo optimize ASAP private static Task UserLeft(IGuildUser user)
private static async Task UserLeft(IGuildUser user)
{ {
try var _ = Task.Run(async () =>
{ {
var conf = GetOrAddSettingsForGuild(user.GuildId);
if (!conf.SendChannelByeMessage) return;
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
if (channel == null) //maybe warn the server owner that the channel is missing
return;
var msg = conf.ChannelByeMessageText.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
if (string.IsNullOrWhiteSpace(msg))
return;
try try
{ {
var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); var conf = GetOrAddSettingsForGuild(user.GuildId);
if (conf.AutoDeleteByeMessagesTimer > 0)
if (!conf.SendChannelByeMessage) return;
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.ByeMessageChannelId);
if (channel == null) //maybe warn the server owner that the channel is missing
return;
CREmbed embedData;
if (CREmbed.TryParse(conf.ChannelByeMessageText, out embedData))
{ {
toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer); embedData.PlainText = embedData.PlainText?.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
embedData.Description = embedData.Description?.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
embedData.Title = embedData.Title?.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
try
{
var toDelete = await channel.EmbedAsync(embedData.ToEmbed(), embedData.PlainText ?? "").ConfigureAwait(false);
if (conf.AutoDeleteByeMessagesTimer > 0)
{
toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer);
}
}
catch (Exception ex) { _log.Warn(ex); }
}
else
{
var msg = conf.ChannelByeMessageText.Replace("%user%", user.Username).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
if (string.IsNullOrWhiteSpace(msg))
return;
try
{
var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false);
if (conf.AutoDeleteByeMessagesTimer > 0)
{
toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer);
}
}
catch (Exception ex) { _log.Warn(ex); }
} }
} }
catch (Exception ex) { _log.Warn(ex); } catch { }
} });
catch { } return Task.CompletedTask;
} }
private static async Task UserJoined(IGuildUser user) private static Task UserJoined(IGuildUser user)
{ {
try var _ = Task.Run(async () =>
{ {
var conf = GetOrAddSettingsForGuild(user.GuildId); try
if (conf.SendChannelGreetMessage)
{ {
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId); var conf = GetOrAddSettingsForGuild(user.GuildId);
if (channel != null) //maybe warn the server owner that the channel is missing
if (conf.SendChannelGreetMessage)
{ {
var msg = conf.ChannelGreetMessageText.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name); var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
if (!string.IsNullOrWhiteSpace(msg)) if (channel != null) //maybe warn the server owner that the channel is missing
{ {
try
CREmbed embedData;
if (CREmbed.TryParse(conf.ChannelGreetMessageText, out embedData))
{ {
var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false); embedData.PlainText = embedData.PlainText?.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
if (conf.AutoDeleteGreetMessagesTimer > 0) embedData.Description = embedData.Description?.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
embedData.Title = embedData.Title?.Replace("%user%", user.ToString()).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
try
{ {
toDelete.DeleteAfter(conf.AutoDeleteGreetMessagesTimer); var toDelete = await channel.EmbedAsync(embedData.ToEmbed(), embedData.PlainText ?? "").ConfigureAwait(false);
if (conf.AutoDeleteGreetMessagesTimer > 0)
{
toDelete.DeleteAfter(conf.AutoDeleteGreetMessagesTimer);
}
}
catch (Exception ex) { _log.Warn(ex); }
}
else
{
var msg = conf.ChannelGreetMessageText.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
if (!string.IsNullOrWhiteSpace(msg))
{
try
{
var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false);
if (conf.AutoDeleteGreetMessagesTimer > 0)
{
toDelete.DeleteAfter(conf.AutoDeleteGreetMessagesTimer);
}
}
catch (Exception ex) { _log.Warn(ex); }
} }
} }
catch (Exception ex) { _log.Warn(ex); }
} }
} }
}
if (conf.SendDmGreetMessage) if (conf.SendDmGreetMessage)
{
var channel = await user.CreateDMChannelAsync();
if (channel != null)
{ {
var msg = conf.DmGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name); var channel = await user.CreateDMChannelAsync();
if (!string.IsNullOrWhiteSpace(msg))
if (channel != null)
{ {
await channel.SendConfirmAsync(msg).ConfigureAwait(false); CREmbed embedData;
if (CREmbed.TryParse(conf.ChannelGreetMessageText, out embedData))
{
embedData.PlainText = embedData.PlainText?.Replace("%user%", user.ToString()).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
embedData.Description = embedData.Description?.Replace("%user%", user.ToString()).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
embedData.Title = embedData.Title?.Replace("%user%", user.ToString()).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
try
{
await channel.EmbedAsync(embedData.ToEmbed(), embedData.PlainText ?? "").ConfigureAwait(false);
}
catch (Exception ex) { _log.Warn(ex); }
}
else
{
var msg = conf.DmGreetMessageText.Replace("%user%", user.ToString()).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
if (!string.IsNullOrWhiteSpace(msg))
{
await channel.SendConfirmAsync(msg).ConfigureAwait(false);
}
}
} }
} }
} }
} catch { }
catch { } });
return Task.CompletedTask;
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]

View File

@ -11,6 +11,8 @@ using NLog;
using System.Diagnostics; using System.Diagnostics;
using Discord.WebSocket; using Discord.WebSocket;
using System; using System;
using Newtonsoft.Json;
using NadekoBot.DataStructures;
namespace NadekoBot.Modules.CustomReactions namespace NadekoBot.Modules.CustomReactions
{ {
@ -69,7 +71,22 @@ namespace NadekoBot.Modules.CustomReactions
if (reaction != null) if (reaction != null)
{ {
if (reaction.Response != "-") if (reaction.Response != "-")
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } {
CREmbed crembed;
if (CREmbed.TryParse(reaction.Response, out crembed))
{
try { await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "").ConfigureAwait(false); }
catch (Exception ex)
{
_log.Warn("Sending CREmbed failed");
_log.Warn(ex);
}
}
else
{
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
}
}
ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++old); ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++old);
return true; return true;
@ -91,7 +108,20 @@ namespace NadekoBot.Modules.CustomReactions
if (greaction != null) if (greaction != null)
{ {
try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } CREmbed crembed;
if (CREmbed.TryParse(greaction.Response, out crembed))
{
try { await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "").ConfigureAwait(false); }
catch (Exception ex)
{
_log.Warn("Sending CREmbed failed");
_log.Warn(ex);
}
}
else
{
try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
}
ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++old); ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++old);
return true; return true;
} }