我正在使用 Discord.net,但我无法让此代码工作...
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
namespace NetflixManager
{
class Program
{
private readonly DiscordSocketClient _client;
static void Main(string[] args)
{
new Program().MainAsync().GetAwaiter().GetResult();
}
public Program()
{
_client = new DiscordSocketClient();
_client.Log += LogAsync;
_client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync;
}
public async Task MainAsync()
{
await _client.LoginAsync(TokenType.Bot, File.ReadAllText("Token.txt"));
await _client.StartAsync();
await Task.Delay(-1);
}
private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
private Task ReadyAsync()
{
Console.WriteLine($"{_client.CurrentUser} is connected!");
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(SocketMessage message)
{
// The bot should never respond to itself.
if (message.Author.Id == _client.CurrentUser.Id)
return;
// The bot should not reply to private messages
if (message.Channel.Name.StartsWith("@"))
return;
// The bot should not reply to bots
if (message.Author.IsBot)
return;
// The bot should not reply to a webhook
if (message.Author.IsWebhook)
return;
// Commands
if (message.Content.StartsWith("!create"))
{
if (message.Author is SocketGuildUser socketUser)
{
SocketGuild socketGuild = socketUser.Guild;
SocketRole socketRole = socketGuild.GetRole(772788208500211724);
if (socketUser.Roles.Any(r => r.Id == socketRole.Id))
{
await message.Channel.SendMessageAsync("The user '" + socketUser.Username + "' already has the role '" + socketRole.Name + "'!");
}
else
{
await socketUser.AddRoleAsync(socketRole);
await message.Channel.SendMessageAsync("Added Role '" + socketRole.Name + "' to '" + socketUser.Username + "'!");
}
}
}
if (message.Content == "!ping")
await message.Channel.SendMessageAsync("pong!");
}
}
}
我想监控是否有人在公会的任何聊天中写入“!create”,然后检查发送消息的人是否具有名为“游戏通知”的角色(Id:772788208500211724)。 如果此人确实具有该角色,则应将其输出到发送原始消息的通道:
"The user '<Username>' already has the role '<RoleName>'!"
如果此人没有该角色,则应为其授予该角色并将其输出到发送原始消息的通道:
"Added Role '<RoleName>' to '<Username>'!"
如果我启动机器人,当我没有角色并且我写!创建一个聊天时,它会成功给我角色。当我第二次执行该命令时,它再次给我这个角色。它并没有说我已经有了这个角色。 反之亦然: 如果我
在启动机器人时拥有该角色并正确执行命令,则表示我拥有该角色。当我现在手动删除角色并再次执行命令时,它仍然说我拥有该角色。 有什么想法可以解决这个问题吗?
使用 Discord.net Nu-Get Packet v2.2.0
,而不是当前的方法。但是,这样做并不能解决您的问题。 如果您注意到与用户相关的奇怪行为(您也是如此),那么很可能是由于最近的特权意图更新所致。 Discord.NET 缓存(下载)用户并使用 GuildUserUpdated 等事件在后台保持此缓存最新。如果没有公会成员的意图,Discord.NET 就无法保持其用户缓存最新,从而导致诸如此类的问题。
要解决此问题,请在 Discord 开发者门户上的机器人页面的“机器人”选项卡上启用公会成员特权意图。
如果这不起作用,请使用 Discord.NET 的夜间版本,并在
DiscordSocketConfig中指定您需要的所有意图。要使用夜间版本,请在 NuGet 包管理器上添加 https://www.myget.org/F/discord-net/api/v3/index.json 作为包源。 这是我的 DiscordSocketConfig,它指定网关意图(仅在夜间可用):
new DiscordSocketConfig
{
TotalShards = _totalShards,
MessageCacheSize = 0,
ExclusiveBulkDelete = true,
AlwaysDownloadUsers = _config.FillUserCache,
LogLevel = Discord.LogSeverity.Info,
GatewayIntents =
GatewayIntents.Guilds |
GatewayIntents.GuildMembers |
GatewayIntents.GuildMessageReactions |
GatewayIntents.GuildMessages |
GatewayIntents.GuildVoiceStates
});
如果您需要更多帮助,我建议加入非官方 Discord API 服务器