【发布时间】:2017-10-28 02:32:15
【问题描述】:
我正在制作一个不和谐的机器人,并且几乎没有使用 c# 编码的经验。我有一个用于基本命令的基本结构,但我无法实现一个无限循环计时器,它将每小时在代码中运行一次命令。我在命令起作用的地方已经足够远了,而且是随机的,我只是无法获得每小时计时器。非常感谢任何帮助。
using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bleachedbot
{
class MyBot
{
DiscordClient discord;
CommandService commands;
Random rand;
string[] bestBleach;
public MyBot()
{
rand = new Random();
bestBleach = new string[]
{
//All images in the folder.
"imagetests/Bleach 001.jpg",
"imagetests/Bleach 002.jpg",
"imagetests/Bleach 003.jpg"
};
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("help")
.Do(async (e) =>
{
await e.Channel.SendMessage("Commands: ");
});
RegiseterAutoBleachCommand();
discord.ExecuteAndWait(async () =>
{
await discord.Connect("<TOKEN>", TokenType.Bot);
});
}
private void RegiseterAutoBleachCommand()
{
commands.CreateCommand("foldertest")
.Do(async (e) =>
{
int randomHentaiIndex = rand.Next(bestBleach.Length);
string bleachToPost = bestBleach[randomBleachIndex];
await e.Channel.SendFile(bleachToPost);
});
}
private void Log(object sender, LogMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}
【问题讨论】:
标签: c# timer infinite-loop discord discord.net