如何使用Discord.net v0.9.6在c#中每24小时重复一次discord命令?

时间:2017-03-20 03:39:08

标签: c# discord discord.net

如何设置命令每24小时自动重复一次?

private void RegisterMemeCommand()
    {
        commands.CreateCommand("meme")
            .Do(async (e) =>
            {
                int randomMemeIndex = rand.Next(FullmetalMemes.Length);
                string memeToPost = FullmetalMemes[randomMemeIndex];
                await e.Channel.SendFile(memeToPost);
            });
    }

我试图让它在每天中午自行执行,而不必进入我的不和谐并输入meme。

1 个答案:

答案 0 :(得分:2)

您不需要创建命令。您可以直接将消息发送到具有其ID的频道。

示例代码:

    const ulong serverId = 38297389272897UL; // the id of your server
    const ulong channelId = 78346982689343UL; // the id of the channel

    Server findServer(ulong id)
    {
        foreach(Server server in discord.Servers) // discord is your DiscorClient instance
        {
            if (server.Id == serverId)
                return server;
        }
        return null;
    }

    Channel findTextChannel(Server server, ulong id)
    {
        foreach(Channel channel in server.TextChannels)
        {
            if (channel.Id == channelId)
                return channel;
        }
        return null;
    }

    private Channel channel;
    private System.Threading.Timer timer;

    void load()
    {
        Server server = findServer(serverId);
        if (server != null)
        {
            channel = findTextChannel(server, channelId);
            if (channel != null)
                timer = new System.Threading.Timer(send, null, 0, 1000 * 60 * 60 * 24); // 24 hour interval
        }
    }

    void send(object state)
    {
        channel.SendMessage("your message");
    }

右键单击服务器或频道,然后单击“复制ID”,即可获取Discord中服务器和频道的ID。如果您没有此选项,请确保已在设置中检查了开发者模式。