作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Discord.Net 创建了一个 Discord 机器人,它观察来自多个公会的多个语音 channel 。我在听 UserVoiceStateUpdated
事件,因为每当用户加入被静音的观察语音 channel 时,该用户应该被机器人静音。每当用户离开观察到的语音信道时,该用户应该被取消静音。
我有一个内存缓存,用于保存有关所有观察到的语音 channel 的信息。每个信息对象提供这些信息
public class ObservedVoiceChannelInfo
{
public ulong VoiceChannelId { get; set; }
public bool Muted { get; set; }
public List<ulong> MutedUserIds { get; set; } = new List<ulong>();
}
对于事件部分,我考虑了以下几点:
observedVoiceChannelsCache
是具有观察语音 channel 的内存缓存字典)
private async Task OnUserVoiceStateUpdated(SocketUser socketUser, SocketVoiceState oldSocketVoiceState,
SocketVoiceState newSocketVoiceState)
{
SocketGuildUser socketGuildUser = socketUser as SocketGuildUser;
if (newSocketVoiceState.IsMuted &&
newSocketVoiceState.VoiceChannel == null &&
_observedVoiceChannelsCache.TryGetValue(oldSocketVoiceState.VoiceChannel.Id,
out ObservedVoiceChannelInfo oldObservedVoiceChannelInfo) &&
oldObservedVoiceChannelInfo.Muted)
{
bool userRemovedFromMuteList = oldObservedVoiceChannelInfo.MutedUserIds.Remove(socketGuildUser.Id);
if (userRemovedFromMuteList)
await SetUserVoiceState(socketGuildUser, false);
}
else if (!newSocketVoiceState.IsMuted &&
newSocketVoiceState.VoiceChannel != null &&
_observedVoiceChannelsCache.TryGetValue(newSocketVoiceState.VoiceChannel.Id,
out ObservedVoiceChannelInfo newObservedVoiceChannelInfo) &&
newObservedVoiceChannelInfo.Muted)
{
await SetUserVoiceState(socketGuildUser, true);
newObservedVoiceChannelInfo.MutedUserIds.Add(socketGuildUser.Id);
}
}
private async Task SetUserVoiceState(SocketGuildUser socketGuildUser, bool muted)
{
await socketGuildUser.ModifyAsync(guildUserProperties => { guildUserProperties.Mute = muted; });
}
else 块似乎工作正常(加入观察到的静音 channel )。但是第一个 if 块(离开观察到的静音 channel )不起作用。用户从列表中删除,但在调用
SetUserVoiceState
时与
false
用户不会被机器人取消静音。我可以通过加入观察到的静音语音 channel ,离开它并加入另一个未观察到的语音 channel 来重现它。那我还是哑巴了。
最佳答案
注意:重要注释下的变量需要在bot启动时进行初始化。
该脚本通过跟踪静音用户来工作,并且不会取消已被管理员静音的用户的静音。 bot 只能在处于事件状态时跟踪静音用户,因此您可能希望为 SelfOrAdminMutedUsersIds 列表包含一个保存功能,或者开发一种从 discord 接收该列表的方法。
public class ObservedVoiceChannelInfo
{
public ulong VoiceChannelId { get; set; }
public bool Muted { get; set; }
public List<ulong> MutedUserIds { get; set; } = new List<ulong>();
}
public class ObservedGuildsMutingInfo
{
public ulong GuildId { get; set; }
public List<ulong> SelfOrAdminMutedUsersIds { get; set; }
}
//IMPORTANT: make sure to initialize on bot start:
public Dictionary<ulong, ObservedGuildsMutingInfo> _observedGuildsMutingInfoCache = new Dictionary<ulong, ObservedGuildsMutingInfo>();
//IMPORTANT: make sure to initialize on bot start:
public Dictionary<ulong, ObservedVoiceChannelInfo> _observedVoiceChannelsCache = new Dictionary<ulong, ObservedGuildsMutingInfo>();
private async Task OnUserVoiceStateUpdated(SocketUser socketUser, SocketVoiceState oldSocketVoiceState,
SocketVoiceState newSocketVoiceState)
{
SocketGuildUser socketGuildUser = socketUser as SocketGuildUser;
if (!oldSocketVoiceState.IsMuted && newSocketVoiceState.IsMuted &&
_observedGuildsMutingInfoCache.TryGetValue(newSocketVoiceState.VoiceChannel.Guild.Id, out ObservedGuildsMutingInfo newObservedMutinglInfo))
{
// a user was muted by someone
if (_observedVoiceChannelsCache.TryGetValue(oldSocketVoiceState.VoiceChannel.Id, out ObservedVoiceChannelInfo oldObservedVoiceChannelInfo)&&
!oldObservedVoiceChannelInfo.MutedUserIds.Contains(socketGuildUser.Id))
{
// the user was not muted by the bot
// meaning that the user muted theirselfs or were muted by admin
newObservedMutingInfo.SelfOrAdminMutedUsersIds.Add(socketGuildUser.Id);
}
}
else if (!oldSocketVoiceState.IsMuted && newSocketVoiceState.IsMuted &&
_observedGuildsMutingInfoCache.TryGetValue(newSocketVoiceState.VoiceChannel.Guild.Id, out ObservedGuildsMutingInfo newObservedMutinglInfo))
{
// a user was un-muted by someone
newObservedMutingInfo.SelfOrAdminMutedUsersIds.Remove(socketGuildUser.Id);
}
else if (!newSocketVoiceState.IsMuted &&
newSocketVoiceState.VoiceChannel != null &&
_observedVoiceChannelsCache.TryGetValue(newSocketVoiceState.VoiceChannel.Id,
out ObservedVoiceChannelInfo newObservedVoiceChannelInfo) &&
!newObservedVoiceChannelInfo.Muted)
{
// user has joined a un-muted channel
if(_observedGuildsMutingInfoCache.TryGetValue(newSocketVoiceState.VoiceChannel.Guild.Id, out ObservedGuildsMutingInfo newObservedGuildsMutingInfo)&&
!newObservedGuildsMutingInfo.SelfOrAdminMutedUsersIds.Contains(socketGuildUser.Id))
{
await SetUserVoiceState(socketGuildUser, false);
}
}
else if (newSocketVoiceState.IsMuted &&
newSocketVoiceState.VoiceChannel == null &&
_observedVoiceChannelsCache.TryGetValue(oldSocketVoiceState.VoiceChannel.Id,
out ObservedVoiceChannelInfo oldObservedVoiceChannelInfo) &&
oldObservedVoiceChannelInfo.Muted)
{
oldObservedVoiceChannelInfo.MutedUserIds.Remove(socketGuildUser.Id);
}
else if (!newSocketVoiceState.IsMuted &&
newSocketVoiceState.VoiceChannel != null &&
_observedVoiceChannelsCache.TryGetValue(newSocketVoiceState.VoiceChannel.Id,
out ObservedVoiceChannelInfo newObservedVoiceChannelInfo) &&
newObservedVoiceChannelInfo.Muted)
{
await SetUserVoiceState(socketGuildUser, true);
newObservedVoiceChannelInfo.MutedUserIds.Add(socketGuildUser.Id);
}
}
private async Task SetUserVoiceState(SocketGuildUser socketGuildUser, bool muted)
{
await socketGuildUser.ModifyAsync(guildUserProperties => { guildUserProperties.Mute = muted; });
}
我希望这会有所帮助。
关于c# - 将 guildUserProperties.Mute 设置为 false 时,Bot 不会取消用户静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65368607/
我使用 Discord.Net 创建了一个 Discord 机器人,它观察来自多个公会的多个语音 channel 。我在听 UserVoiceStateUpdated事件,因为每当用户加入被静音的观察
我是一名优秀的程序员,十分优秀!