问题描述
我正在使用 discord.Net 并观察多个语音频道。我跟踪它们是否被机器人静音并存储与此语音通道相关的命令消息 ID。
public sealed class ObservedVoiceChannel
{
public ulong VoiceChannelId { get; set; }
public bool IsMuted { get; set; }
public ulong MessageId { get; set; }
}
我正在使用一个服务作为内存缓存
public sealed class ObservedVoiceChannelsCache : Dictionary<ulong,ObservedVoiceChannel>
{
}
每当我创建命令消息时,我都会添加一个反应作为按钮来静音特定的语音通道。通过切换反应,我可以决定将语音频道静音/取消静音。如果静音状态设置为 true
,机器人会将所有用户静音。如果状态设置为 false
,该语音频道中的所有用户都将被取消静音。
public sealed class ReactionAddedEventHandler
{
private const string MutedEmoji = "\uD83D\uDD07";
private const string UnMutedEmoji = "\uD83D\uDD0A";
private readonly ObservedVoiceChannelsCache _observedVoiceChannelsCache;
public ReactionAddedEventHandler(ObservedVoiceChannelsCache observedVoiceChannelsCache)
{
_observedVoiceChannelsCache = observedVoiceChannelsCache;
}
public async Task OnReactionAdded(
Cacheable<IUserMessage,ulong> cacheable,ISocketMessageChannel socketMessageChannel,SocketReaction socketReaction)
{
string emoteName = socketReaction.Emote.Name;
bool emoteIsMutedEmoji = emoteName == MutedEmoji;
bool emoteIsUnMutedEmoji = emoteName == UnMutedEmoji;
if (emoteIsMutedEmoji || emoteIsUnMutedEmoji)
{
ObservedVoiceChannel observedVoiceChannel =
_observedVoiceChannelsCache.Values.FirstOrDefault(currentObservedVoiceChannel =>
currentObservedVoiceChannel.MessageId == socketReaction.MessageId);
if (observedVoiceChannel != null)
{
IMessage message = await socketMessageChannel.GetMessageAsync(cacheable.Id);
if (message.Reactions[socketReaction.Emote].ReactionCount > 1)
{
SocketGuildChannel socketGuildChannel = socketMessageChannel as SocketGuildChannel;
SocketVoiceChannel socketVoiceChannel = socketGuildChannel.Guild.GetVoiceChannel(observedVoiceChannel.VoiceChannelId);
IVoiceChannel voiceChannel = socketVoiceChannel as IVoiceChannel;
IEnumerable<IGuildUser> voiceChannelUsers = await voiceChannel
.GetUsersAsync()
.FlattenAsync();
Emoji mutedEmoji = new Emoji(MutedEmoji);
Emoji unMutedEmoji = new Emoji(UnMutedEmoji);
if (observedVoiceChannel.IsMuted && emoteIsUnMutedEmoji)
{
// switch mode and unmute everyone in that voice channel
List<Task> tasks = voiceChannelUsers
.Select(voiceChannelUser => voiceChannelUser.ModifyAsync(guildUserProperties => guildUserProperties.Mute = false))
.ToList();
tasks.Add(message.RemoveAllReactionsForEmoteAsync(unMutedEmoji));
tasks.Add(message.AddReactionAsync(mutedEmoji));
await Task.WhenAll(tasks);
observedVoiceChannel.IsMuted = false;
}
else if (!observedVoiceChannel.IsMuted && emoteIsMutedEmoji)
{
// switch mode and mute everyone in that voice channel
List<Task> tasks = voiceChannelUsers
.Select(voiceChannelUser => voiceChannelUser.ModifyAsync(guildUserProperties => guildUserProperties.Mute = true))
.ToList();
tasks.Add(message.RemoveAllReactionsForEmoteAsync(mutedEmoji));
tasks.Add(message.AddReactionAsync(unMutedEmoji));
await Task.WhenAll(tasks);
observedVoiceChannel.IsMuted = true;
}
}
}
}
}
}
这个处理程序工作正常,虽然它并不完美。假设观察到的语音通道处于静音状态,返回 false
并且该语音通道中有 8-14 个人。
当对静音表情符号做出反应大约 4 秒时,没有任何反应。之后,每个人都会被机器人静音。我认为这是因为抢占式速率限制。但如果能立即发生就好了……当对大约 12 秒 的取消静音表情做出反应时,什么也没有发生。之后,人们会一步步慢慢静音。这与静音过程不同,因此静音似乎比取消静音更快。
有人知道这个代码是否可以改进或者是否有任何代码导致问题?我想在性能/执行时间方面加快速度。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)