如何在不诉诸任务延迟的情况下保持我的不和谐机器人登录?



我目前正在运行一个 Discord 机器人,但除非我在用于登录的方法中添加await Task.Delay(-1),否则它不会保持登录状态。我怀疑这在我的代码的后面部分给我带来了一些问题。

以下是我的连接方式:

public override async Task ConnectToClientAsync()
{
await this._client.LoginAsync(TokenType.Bot, this.Token);
await this._client.StartAsync();
await Task.Delay(-1);
}

如何在不使用Task.Delay(-1)的情况下保持我的机器人在线?它只是登录,一旦成功完成,它就会终止程序(因为它不会在没有此延迟的情况下保持登录状态(。

主要

class Program
{
static async Task Main(string[] args)
{
var newBot = new WelcomeBot(674094599903641628);
await newBot.LaunchBot();
}
}

欢迎机器人

public class WelcomeBot : BasicBot, IMessageWriter, IMessageReader
{
private ulong _channelId;
public WelcomeBot(ulong channelId) : base()
{
this._client = new DiscordSocketClient();
this._channelId = channelId;
ConfigureBotFunctionality();
}
private void ConfigureBotFunctionality()
{
Interact_UserEntryAndExit();
}
private void Interact_UserEntryAndExit()
{
this._client.UserJoined += AnnounceUserJoiningAsync;
this._client.UserLeft += AnnounceUserLeavingAsync;
this._client.Connected += OnConnectAnnouncementAsync;
}
public async Task LaunchBot()
{
await ConnectToClientAsync();
}
public override async Task ConnectToClientAsync()
{
await this._client.LoginAsync(TokenType.Bot, this.Token);
await this._client.StartAsync();
await Task.Delay(-1);
}
private async Task OnConnectAnnouncementAsync()
{
// ..
}
private async Task AnnounceUserLeavingAsync(SocketGuildUser leavingUser)
{
//..
}
private async Task AnnounceUserJoiningAsync(SocketGuildUser joinedUser)
{
//..
}
// Other functions.
}

只需添加一个小延迟,300 或 400 之类的。

最新更新