【问题标题】:How to change discord bots game activity randomly after a interval如何在间隔后随机更改不和谐机器人的游戏活动
【发布时间】:2020-09-02 01:03:53
【问题描述】:

我正在尝试让我的机器人每隔一段时间更改其游戏状态。

这是我尝试过的: 使它成为一个命令这工作但不能使用它,因为该命令总是在运行,因为循环使其他命令无法运行。

[Command("randomplaying")]
        [RequireUserPermission(GuildPermission.MentionEveryone)]
        private async Task randomPlaying()
        {
            const int delay = 3000;

            Random rand = new Random();

            string[] serverActivity = File.ReadAllLines(Server.GameActivity.servergameactivitypath);

            for (; ; )
            {
                int randomIndex = rand.Next(serverActivity.Length);
                string gameActityText = serverActivity[randomIndex];
                await Task.Delay(delay);
                await Context.Client.SetGameAsync(gameActityText, "", ActivityType.Playing);
            }
        }

尝试在 Program.cs 中执行此操作

private async Task randomPlaying()
        {
            const int delay = 3000;

            Random rand = new Random();

            string[] serverActivity = File.ReadAllLines(Commands.Server.GameActivity.servergameactivitypath);

            for (; ; )
            {
                int randomIndex = rand.Next(serverActivity.Length);
                string gameActityText = serverActivity[randomIndex];
                await Task.Delay(delay);
                await Context.Client.SetGameAsync(gameActityText, "", ActivityType.Playing);
            }
        }

并用

调用它
await randomPlaying();

这个方法什么都不做,甚至不显示它的工作原理。

servergameactivitypath 代码是

public static string servergameactivitypath = AppDomain.CurrentDomain.BaseDirectory + "/config/gameactivity.txt";


        public static void createGameActivityTXT()
        {
            File.WriteAllText(servergameactivitypath, "Hello There\nTest1\nTest2\nTest3");
        }

    }

【问题讨论】:

  • 上下文只存在于命令模块中。只需使用您在 program.cs 中登录的客户端实例
  • @Anu6is 好的谢谢你我已经修好了。

标签: c# discord.net


【解决方案1】:

用途:


    private async Task randomPlaying()
        {
            const int delay = 3000;

            Random rand = new Random();

            string[] serverActivity = File.ReadAllLines(Commands.Server.GameActivity.servergameactivitypath);

            for (; ; )
            {
                int randomIndex = rand.Next(serverActivity.Length);
                string gameActityText = serverActivity[randomIndex];
                await Task.Delay(delay);
                await _client.SetGameAsync(gameActityText, "", ActivityType.Playing);
            }
        } 

使用上面的 Task.Delay 调用 MainAsync()

await randomPlaying()

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 2017-05-10
    • 2017-11-15
    • 2022-01-25
    • 2021-08-07
    • 2021-03-10
    • 2021-05-14
    • 2021-05-10
    • 2021-06-02
    相关资源
    最近更新 更多