Discord: Allow customising .who embed response with plugin event

This commit is contained in:
UnknownShadow200 2023-12-19 23:06:31 +11:00
parent 07a1bd8925
commit daf4f0c05a
3 changed files with 27 additions and 5 deletions

View File

@ -364,17 +364,18 @@ namespace MCGalaxy.SQL
return data;
}
public unsafe static string FromUTF8(IntPtr ptr, int len) {
public static string FromUTF8(IntPtr ptr, int len) {
if (ptr == IntPtr.Zero) return "";
byte* mem = (byte*)ptr;
if (len < 0) {
len = 0;
while (mem[len] != 0) { len++; }
while (Marshal.ReadByte(ptr, len) != 0) { len++; }
}
if (len == 0) return "";
return utf8.GetString(mem, len);
byte[] data = new byte[len];
Marshal.Copy(ptr, data, 0, len);
return utf8.GetString(data, 0, len);
}
public static DateTime ToDateTime(string text) {

View File

@ -20,6 +20,7 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using MCGalaxy.Config;
using MCGalaxy.Events;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Games;
using MCGalaxy.Tasks;
@ -27,6 +28,23 @@ using MCGalaxy.Util;
namespace MCGalaxy.Modules.Relay.Discord
{
public delegate void OnSendingWhoEmbed(DiscordBot bot, RelayUser user, ref ChannelSendEmbed embed);
/// <summary> Called when sending an embed response to a .who message from Discord </summary>
public sealed class OnSendingWhoEmbedEvent : IEvent<OnSendingWhoEmbed>
{
public static void Call(DiscordBot bot, RelayUser user, ref ChannelSendEmbed embed) {
IEvent<OnSendingWhoEmbed>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++)
{
try {
items[i].method(bot, user, ref embed);
} catch (Exception ex) {
LogHandlerException(ex, items[i]);
}
}
}
}
sealed class DiscordUser : RelayUser
{
public string ReferencedUser;
@ -511,7 +529,9 @@ namespace MCGalaxy.Modules.Relay.Discord
ConvertMessage(FormatPlayers(p, e))
);
}
AddGameStatus(embed);
OnSendingWhoEmbedEvent.Call(this, p.User, ref embed);
Send(embed);
}

View File

@ -522,7 +522,8 @@ namespace MCGalaxy.Modules.Relay
return Group.DefaultRank;
}
protected sealed class RelayPlayer : Player {
protected sealed class RelayPlayer : Player
{
public readonly string ChannelID;
public readonly RelayUser User;
public readonly RelayBot Bot;