Discord.net C# |下载用户似乎无法正常工作
Discord.net C# | Downloading Users seemingly not working correctly
我的程序似乎没有正确下载用户。配置中添加了“AlwaysDownloadUsers = true”,但它似乎无法正常工作。启动机器人时,似乎并非所有用户都在下载,因为程序总是 returns“未找到用户”,直到用户开始输入或发送消息...
我认为我的代码总体上很糟糕,因为我使用了一些 sources/tutorials 并将代码混合在一起,所以我很高兴收到各种反馈:)
主程序:
class Program
{
static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public DiscordSocketClient _client;
public CommandService _command;
public LoggingService _log;
public CommandHandler _handler;
public async Task MainAsync()
{
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100
};
_client = new DiscordSocketClient(config);
_command = new CommandService();
_log = new LoggingService(_client, _command);
_handler = new CommandHandler(_client, _command);
var token = File.ReadAllText("token.txt");
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
_client.Ready += Ready;
await Task.Delay(-1);
}
public async Task Ready()
{
Console.WriteLine("Bot is Ready!");
await _handler.InstallCommandsAsync();
}
}
命令处理程序:
public class CommandHandler
{
private readonly DiscordSocketClient _client;
private readonly CommandService _command;
public CommandHandler(DiscordSocketClient client, CommandService command)
{
_client = client;
_command = command;
}
public async Task InstallCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _command.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var msg = arg as SocketUserMessage;
if (msg == null) return;
int argPos = 0;
if (msg.HasCharPrefix('.', ref argPos) && !(msg.Author.IsBot))
{
var _context = new SocketCommandContext(_client, msg);
var result = await _command.ExecuteAsync(_context, argPos, services: null);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
if (result.Error.Equals(CommandError.UnmetPrecondition))
await msg.Channel.SendMessageAsync(result.ErrorReason);
}
}
}
命令 Class 未包含在内,因为我很确定它没有错
开发人员门户中的成员意图已启用,但 DiscordSocketConfig 中未启用
所以我在配置中添加了“Gateway.Intents = GatewayIntents.All”并且它现在可以正常工作了
不工作:
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
};
现在工作:
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
GatewayIntents = GatewayIntents.All
};
谢谢 Anu6is
我的程序似乎没有正确下载用户。配置中添加了“AlwaysDownloadUsers = true”,但它似乎无法正常工作。启动机器人时,似乎并非所有用户都在下载,因为程序总是 returns“未找到用户”,直到用户开始输入或发送消息...
我认为我的代码总体上很糟糕,因为我使用了一些 sources/tutorials 并将代码混合在一起,所以我很高兴收到各种反馈:)
主程序:
class Program
{
static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public DiscordSocketClient _client;
public CommandService _command;
public LoggingService _log;
public CommandHandler _handler;
public async Task MainAsync()
{
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100
};
_client = new DiscordSocketClient(config);
_command = new CommandService();
_log = new LoggingService(_client, _command);
_handler = new CommandHandler(_client, _command);
var token = File.ReadAllText("token.txt");
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
_client.Ready += Ready;
await Task.Delay(-1);
}
public async Task Ready()
{
Console.WriteLine("Bot is Ready!");
await _handler.InstallCommandsAsync();
}
}
命令处理程序:
public class CommandHandler
{
private readonly DiscordSocketClient _client;
private readonly CommandService _command;
public CommandHandler(DiscordSocketClient client, CommandService command)
{
_client = client;
_command = command;
}
public async Task InstallCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _command.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var msg = arg as SocketUserMessage;
if (msg == null) return;
int argPos = 0;
if (msg.HasCharPrefix('.', ref argPos) && !(msg.Author.IsBot))
{
var _context = new SocketCommandContext(_client, msg);
var result = await _command.ExecuteAsync(_context, argPos, services: null);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
if (result.Error.Equals(CommandError.UnmetPrecondition))
await msg.Channel.SendMessageAsync(result.ErrorReason);
}
}
}
命令 Class 未包含在内,因为我很确定它没有错
开发人员门户中的成员意图已启用,但 DiscordSocketConfig 中未启用
所以我在配置中添加了“Gateway.Intents = GatewayIntents.All”并且它现在可以正常工作了
不工作:
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
};
现在工作:
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
GatewayIntents = GatewayIntents.All
};
谢谢 Anu6is