我只需要一个工作代码行,每五秒发送一条消息,然后在输入另一个命令后停止。例如,如果用户输入“〜raid”,那么机器人将每五秒钟发送一次“RAID RAID”,并在用户“停止”时停止。如果有人能提供帮助那就太棒了。
这是我到目前为止所得到的: MyBot类 { DiscordClient不和谐; CommandService命令;
public MyBot()
{
discord = new DiscordClient(x =>
{
x.LogLevel = LogSeverity.Info;
x.LogHandler = Log;
});
discord.UsingCommands(x =>
{
x.PrefixChar = '~';
x.AllowMentionPrefix = true;
});
commands = discord.GetService<CommandService>();
commands.CreateCommand("checked")
.Do(async (e) =>
{
commands.CreateCommand("weewoo")
.Do(async (e) =>
{
await e.Channel.SendMessage("**WEE WOO**");
});
discord.ExecuteAndWait(async () =>
{
await discord.Connect("discordkeyhere", TokenType.Bot);
});
}
public void Log(object sender, LogMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}
答案 0 :(得分:0)
这是一个小例子,你可以做到这一点。 在这种情况下,您可以使用Start和Stop方法从外部启动和停止机器人。
class MyBot
{
public MyBot()
{
}
CancellationTokenSource cts;
public void Start()
{
cts = new CancellationTokenSource();
Task t = Task.Run(() =>
{
while (!cts.IsCancellationRequested)
{
Console.WriteLine("RAID RAID");
Task.Delay(5000).Wait();
}
}, cts.Token);
}
public void Stop()
{
cts?.Cancel();
}
}
以下是测试MyBot类的代码
static void Main(string[] args)
{
try
{
var b = new MyBot();
while (true)
{
var input = Console.ReadLine();
if (input.Equals("~raid", StringComparison.OrdinalIgnoreCase))
b.Start();
else if (input.Equals("~stop", StringComparison.OrdinalIgnoreCase))
b.Stop();
else if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
Task.Delay(1000);
}
}
catch (Exception)
{
throw;
}
}