Skip to content

Commit

Permalink
Reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Dec 20, 2024
1 parent 6d610f9 commit f14494a
Show file tree
Hide file tree
Showing 26 changed files with 954 additions and 64 deletions.
5 changes: 4 additions & 1 deletion samples/QQBot.Net.Samples.SimpleBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
if (message.Source is not MessageSource.User) return;
if (message.Channel is SocketTextChannel textChannel)
{
IReadOnlyCollection<IThread> threads = await textChannel.Guild.ForumChannels.First().GetThreadsAsync();
await message.AddReactionAsync(new Emote(Emotes.System.Angry));
await Task.Delay(TimeSpan.FromSeconds(5));
IEnumerable<IGuildUser> flattenAsync = await message.GetReactionUsersAsync(new Emote(Emotes.System.Angry)).FlattenAsync();
await message.RemoveReactionAsync(new Emote(Emotes.System.Angry));
}

// IUserMessage msg = await message.ReplyAsync(
Expand Down
32 changes: 32 additions & 0 deletions src/QQBot.Net.Core/Entities/Emotes/EmojiType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace QQBot;

/// <summary>
/// 表示一个表情方式的类型。
/// </summary>
public enum EmojiType
{
/// <summary>
/// 系统表情,小黄脸表情。
/// </summary>
System = 1,

/// <summary>
/// Emoji 表情。
/// </summary>
Emoji = 2,

/// <summary>
/// 超级表情
/// </summary>
SuperEmote = 3,

/// <summary>
/// 商城表情。
/// </summary>
Sticker = 4,

/// <summary>
/// QQ 秀表情。
/// </summary>
QQShow = 6
}
34 changes: 29 additions & 5 deletions src/QQBot.Net.Core/Entities/Emotes/Emote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace QQBot;
public class Emote : IEmote
{
// <faceType=3,faceId="338",ext="eyJ0ZXh0Ijoi5oiR5oOz5byA5LqGIn0="​>
private static readonly Regex EmoteRegex = new("""^<faceType=(?<type>\d+),faceId="(?<id>\d+)",ext="(?<ext>[a-zA-Z0-9+/=]+)"\u200b?>$""", RegexOptions.Compiled);
private static readonly Regex EmoteRegex = new("""^<faceType=(?<type>\d+),faceId="(?<id>\d*)",ext="(?<ext>[a-zA-Z0-9+/=]+)"\u200b?>$""", RegexOptions.Compiled);

/// <inheritdoc />
public string Id { get; }

/// <summary>
/// 获取表情符号的类型。
/// </summary>
public int Type { get; }
public EmojiType Type { get; }

/// <inheritdoc />
public string Name { get; }
Expand All @@ -30,13 +30,37 @@ public class Emote : IEmote
/// <param name="type"> 表情符号的类型。 </param>
/// <param name="id"> 表情符号的 ID。 </param>
/// <param name="name"> 表情符号的显示名称。 </param>
public Emote(int type, string id, string name)
public Emote(EmojiType type, string id, string name)
{
Id = id;
Type = type;
Id = id;
Name = name;
}

/// <summary>
/// 初始化一个 <see cref="Emote"/> 类的新实例。
/// </summary>
/// <param name="emote"> 系统表情符号。 </param>
/// <param name="name"> 表情符号名称。 </param>
public Emote(Emotes.System emote, string? name = null)
{
Type = EmojiType.System;
Id = ((int)emote).ToString();
Name = name ?? Emotes.SystemNames.GetValueRefOrNullRef(emote);
}

/// <summary>
/// 初始化一个 <see cref="Emote"/> 类的新实例。
/// </summary>
/// <param name="emote"> Emoji 表情符号。 </param>
/// <param name="name"> 表情符号名称。 </param>
public Emote(Emotes.Emoji emote, string? name = null)
{
Type = EmojiType.Emoji;
Id = ((int)emote).ToString();
Name = name ?? Emotes.EmojiNames.GetValueRefOrNullRef(emote);
}

/// <summary>
/// 将一个表情符号的原始格式解析为一个 <see cref="Emote"/>。
/// </summary>
Expand All @@ -59,7 +83,7 @@ public static Emote Parse(string text)
string json = Encoding.UTF8.GetString(Convert.FromBase64String(ext));
JsonElement element = JsonSerializer.Deserialize<JsonElement>(json);
string name = element.GetProperty("text").GetString() ?? string.Empty;
return new Emote(type, id, name);
return new Emote((EmojiType)type, id, name);
}

/// <summary>
Expand Down
Loading

0 comments on commit f14494a

Please sign in to comment.