【问题标题】:Hourly auto command for DiscordDiscord 的每小时自动命令
【发布时间】: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


    【解决方案1】:

    如果你想创建一个计时器,你需要包含 system.threathing。 您可以这样做在第一行添加:

    using System.Threading;
    

    之后,您可以在代码中使用命令 Sleep。 睡眠会让您的代码暂停您选择的几秒钟。 这样做:

    Thread.Sleep(5000) //This wil sleep for 5 seconds, because 5000 miliseconds = 5 seconds
    

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2021-03-11
      • 2017-08-31
      • 2021-07-03
      • 2020-01-09
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多