如何将命令设置为每 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);
});
}
我试图让它每天中午自行执行,而不必进入我的不和谐并输入模因。
您不需要创建命令。您可以直接将消息发送到具有 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。如果没有此选项,请确保已在设置中选中开发人员模式。