我正在尝试将我的机器人从Discord.net 2.4.0升级到3.4.0。现在,我的机器人的所有操作都将返回一个401未经授权的异常。
我就是这样设置的。
public static async Task Main(string[] args)
{
var builder = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(x => x
.SetBasePath(GetBasePath())
.Build())
.ConfigureLogging(x => x
.AddConsole()
.SetMinimumLevel(LogLevel.Debug))
.ConfigureServices((context, services) =>
{
var connectionString = context.Configuration.GetConnectionString("DefaultConnectionString");
var serverversion = ServerVersion.AutoDetect(connectionString);
services.AddDbContext<ApplicationContext>(options => { options.UseMySql(connectionString, serverversion); }, ServiceLifetime.Singleton);
services.AddSingleton<LavaNode>();
services.AddSingleton<LavaConfig>();
services.AddSingleton<AudioService>();
services.AddSingleton<SteamService>();
services.AddHostedService<CommandHandler>();
services.AddLavaNode(x =>
{
x.SelfDeaf = false;
x.LogSeverity = LogSeverity.Debug;
});
})
.ConfigureDiscordHost((context, config) =>
{
config.SocketConfig = new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
MessageCacheSize = 200,
GatewayIntents = GatewayIntents.All,
};
config.Token = context.Configuration["Token"];
})
.UseCommandService((context, config) =>
{
config.CaseSensitiveCommands = false;
config.LogLevel = LogSeverity.Debug;
config.DefaultRunMode = RunMode.Sync;
})
.UseConsoleLifetime();
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
这是我在我的控制台上得到的
info: Discord.Addons.Hosting.DiscordHostedService[0]
Discord.NET hosted service is starting
info: Discord.WebSocket.DiscordSocketClient[0]
Discord: Discord.Net v3.4.0 (API v9)
dbug: Discord.Addons.Hosting.CommandServiceRegistrationHost[0]
Registered logger for CommandService
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:UsersDiegosourcereposfernandesdiegoBotaBotabinDebugnet5.0
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
info: Discord.WebSocket.DiscordSocketClient[0]
Gateway: Connecting
dbug: Discord.WebSocket.DiscordSocketClient[0]
Rest: GET gateway: 252,26 ms
warn: Discord.WebSocket.DiscordSocketClient[0]
Gateway: You're using the GuildScheduledEvents gateway intent without listening to any events related to that intent, consider removing the intent from your config.
warn: Discord.WebSocket.DiscordSocketClient[0]
Gateway: You're using the GuildInvites gateway intent without listening to any events related to that intent, consider removing the intent from your config.
info: Discord.WebSocket.DiscordSocketClient[0]
Gateway: Connected
dbug: Discord.WebSocket.DiscordSocketClient[0]
Gateway: Connected to Servidor de Teste
info: Discord.WebSocket.DiscordSocketClient[0]
Gateway: Ready
###### trying to execute the /ping command #######
fail: Discord.Commands.CommandService[0]
Command:
Discord.Commands.CommandException: Error occurred executing "ping" for ?Diego Fernandes?#3566 in Servidor de Teste/geral.
---> Discord.Net.HttpException: The server responded with error 401: 401: Unauthorized
at Discord.Net.Queue.RequestBucket.SendAsync(RestRequest request)
at Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request)
at Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request)
at Discord.API.DiscordRestApiClient.SendJsonAsync[TResponse](String method, String endpoint, Object payload, BucketId bucketId, ClientBucketType clientBucket, RequestOptions options)
at Discord.API.DiscordRestApiClient.CreateMessageAsync(UInt64 channelId, CreateMessageParams args, RequestOptions options)
at Discord.Rest.ChannelHelper.SendMessageAsync(IMessageChannel channel, BaseDiscordClient client, String text, Boolean isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds, MessageFlags flags)
at Bota.Modules.General.PingAsync() in C:UsersDiegosourcereposfernandesdiegoBotaBotaModulesGeneral.cs:line 32
at Discord.Commands.ModuleClassBuilder.<>c__DisplayClass6_0.<<BuildCommand>g__ExecuteCallback|0>d.MoveNext()
--- End of stack trace from previous location ---
at Discord.Commands.CommandInfo.ExecuteInternalAsync(ICommandContext context, Object[] args, IServiceProvider services)
--- End of inner exception stack trace ---
我在MessageReceived
事件中调用await _service.ExecuteAsync(context, argPos, _provider)
,我的命令如下:
public class General : ModuleBase<SocketCommandContext>
{
///...
[Command("ping")]
[RequireBotPermission(ChannelPermission.SendMessages)]
public async Task PingAsync()
{
await Context.Channel.SendMessageAsync("Pong!");
//ReplyAsync("Pong"); produces the same error //
}
///...
}
我仔细检查了我的代币,我可以在网上看到我的机器人。我从discord那里得到了数据,但却无能为力。
问题出在Discord.Addons.Hosting包上。它不知怎么坏了,甚至在清理和重建我的应用程序之后。不管怎样,我重新安装了,它工作了。