C# 不协调的每小时自动命令
我正在为discord制作一个机器人,对c#编码几乎没有经验。我得到了一个基本命令的基本结构,但我不能实现一个无限循环计时器,它将在代码中每小时运行一次命令。我已经做得够多了,命令是随机的,我只是不能得到每小时的计时器。非常感谢您的帮助C# 不协调的每小时自动命令,c#,timer,infinite-loop,discord,discord.net,C#,Timer,Infinite Loop,Discord,Discord.net,我正在为discord制作一个机器人,对c#编码几乎没有经验。我得到了一个基本命令的基本结构,但我不能实现一个无限循环计时器,它将在代码中每小时运行一次命令。我已经做得够多了,命令是随机的,我只是不能得到每小时的计时器。非常感谢您的帮助 using Discord; using Discord.Commands; using System; using System.Collections.Generic; using System.Linq; using System.Text; using
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);
}
}
}
使用Discord;
使用不协调命令;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间漂白机器人
{
类MyBot
{
不和客户不和;
命令服务命令;
随机兰德;
漂白剂;
公共MyBot()
{
rand=新随机数();
bestbleah=新字符串[]
{
//文件夹中的所有图像。
“imagetests/漂白剂001.jpg”,
“imagetests/漂白剂002.jpg”,
“图像测试/漂白剂003.jpg”
};
discord=新的DiscordClient(x=>
{
x、 LogLevel=LogSeverity.Info;
x、 LogHandler=Log;
});
不协调。使用命令(x=>
{
x、 PrefixChar='-';
x、 AllowMationPrefix=true;
});
commands=discord.GetService();
commands.CreateCommand(“帮助”)
.Do(异步(e)=>
{
等待e.Channel.SendMessage(“命令:”);
});
Regiseterauto命令();
discord.ExecuteAndWait(异步()=>
{
等待discord.Connect(“,TokenType.Bot”);
});
}
私有无效Regiseterauto命令()
{
commands.CreateCommand(“foldertest”)
.Do(异步(e)=>
{
int randomHentaiIndex=rand.Next(最佳漂白剂长度);
字符串漂白度=最佳漂白度[随机漂白指数];
等待e.Channel.SendFile(topost);
});
}
私有无效日志(对象发送者,LogMessageEventArgs e)
{
控制台写入线(e.Message);
}
}
}
如果要制作计时器,需要包括system.Threating。
您可以这样做来添加第一行:
using System.Threading;
之后,您可以在代码中使用Sleep命令。
睡眠会让代码暂停几秒钟。
这样做:
Thread.Sleep(5000) //This wil sleep for 5 seconds, because 5000 miliseconds = 5 seconds