.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:
parent
4429687bd5
commit
91efe4db48
91
src/NadekoBot/DataStructures/CREmbed.cs
Normal file
91
src/NadekoBot/DataStructures/CREmbed.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using NadekoBot.Attributes;
|
||||
using NadekoBot.DataStructures;
|
||||
using NadekoBot.Extensions;
|
||||
using NadekoBot.Services;
|
||||
using NadekoBot.Services.Database;
|
||||
@ -83,8 +84,9 @@ namespace NadekoBot.Modules.Administration
|
||||
return settings;
|
||||
}
|
||||
|
||||
//todo optimize ASAP
|
||||
private static async Task UserLeft(IGuildUser user)
|
||||
private static Task UserLeft(IGuildUser user)
|
||||
{
|
||||
var _ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -95,7 +97,24 @@ namespace NadekoBot.Modules.Administration
|
||||
|
||||
if (channel == null) //maybe warn the server owner that the channel is missing
|
||||
return;
|
||||
|
||||
CREmbed embedData;
|
||||
if (CREmbed.TryParse(conf.ChannelByeMessageText, out embedData))
|
||||
{
|
||||
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;
|
||||
@ -109,10 +128,15 @@ namespace NadekoBot.Modules.Administration
|
||||
}
|
||||
catch (Exception ex) { _log.Warn(ex); }
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static async Task UserJoined(IGuildUser user)
|
||||
private static Task UserJoined(IGuildUser user)
|
||||
{
|
||||
var _ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -122,6 +146,25 @@ namespace NadekoBot.Modules.Administration
|
||||
{
|
||||
var channel = (await user.Guild.GetTextChannelsAsync()).SingleOrDefault(c => c.Id == conf.GreetMessageChannelId);
|
||||
if (channel != null) //maybe warn the server owner that the channel is missing
|
||||
{
|
||||
|
||||
CREmbed embedData;
|
||||
if (CREmbed.TryParse(conf.ChannelGreetMessageText, out embedData))
|
||||
{
|
||||
embedData.PlainText = embedData.PlainText?.Replace("%user%", user.Mention).Replace("%id%", user.Id.ToString()).Replace("%server%", user.Guild.Name);
|
||||
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
|
||||
{
|
||||
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))
|
||||
@ -138,6 +181,7 @@ namespace NadekoBot.Modules.Administration
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (conf.SendDmGreetMessage)
|
||||
{
|
||||
@ -145,7 +189,21 @@ namespace NadekoBot.Modules.Administration
|
||||
|
||||
if (channel != null)
|
||||
{
|
||||
var msg = conf.DmGreetMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name);
|
||||
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);
|
||||
@ -153,7 +211,10 @@ namespace NadekoBot.Modules.Administration
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
[NadekoCommand, Usage, Description, Aliases]
|
||||
|
@ -11,6 +11,8 @@ using NLog;
|
||||
using System.Diagnostics;
|
||||
using Discord.WebSocket;
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using NadekoBot.DataStructures;
|
||||
|
||||
namespace NadekoBot.Modules.CustomReactions
|
||||
{
|
||||
@ -69,7 +71,22 @@ namespace NadekoBot.Modules.CustomReactions
|
||||
if (reaction != null)
|
||||
{
|
||||
if (reaction.Response != "-")
|
||||
{
|
||||
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);
|
||||
return true;
|
||||
@ -90,8 +107,21 @@ namespace NadekoBot.Modules.CustomReactions
|
||||
var greaction = grs[new NadekoRandom().Next(0, grs.Length)];
|
||||
|
||||
if (greaction != null)
|
||||
{
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user