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; return data;
} }
public unsafe static string FromUTF8(IntPtr ptr, int len) { public static string FromUTF8(IntPtr ptr, int len) {
if (ptr == IntPtr.Zero) return ""; if (ptr == IntPtr.Zero) return "";
byte* mem = (byte*)ptr;
if (len < 0) { if (len < 0) {
len = 0; len = 0;
while (mem[len] != 0) { len++; } while (Marshal.ReadByte(ptr, len) != 0) { len++; }
} }
if (len == 0) return ""; 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) { public static DateTime ToDateTime(string text) {

View File

@ -20,6 +20,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using MCGalaxy.Config; using MCGalaxy.Config;
using MCGalaxy.Events;
using MCGalaxy.Events.PlayerEvents; using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Games; using MCGalaxy.Games;
using MCGalaxy.Tasks; using MCGalaxy.Tasks;
@ -27,6 +28,23 @@ using MCGalaxy.Util;
namespace MCGalaxy.Modules.Relay.Discord 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 sealed class DiscordUser : RelayUser
{ {
public string ReferencedUser; public string ReferencedUser;
@ -511,7 +529,9 @@ namespace MCGalaxy.Modules.Relay.Discord
ConvertMessage(FormatPlayers(p, e)) ConvertMessage(FormatPlayers(p, e))
); );
} }
AddGameStatus(embed); AddGameStatus(embed);
OnSendingWhoEmbedEvent.Call(this, p.User, ref embed);
Send(embed); Send(embed);
} }

View File

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