如何每X秒向Discord频道发送一条消息



我创建了一个Discord Bot,可以在其中发送文本命令。例如,键入!start,将发送设置了start参数的消息。机器人可以工作,我可以发送命令,我只是很难弄清楚如何设置计时器并将其与命令绑定。也许将其设置为命令是错误的做法,但我想不出其他发送信息的方法。

这是我迄今为止的代码。我在网上找了好几个地方。很多资源都说我需要包括我使用的System.Timer。我只是很难弄清楚如何将计时器、命令本身以及它进入哪个通道联系在一起。如有任何帮助,我们将不胜感激。

using Discord.Commands;
using System.Timers;
namespace MessageTimerBot.Modules
{
public class Commands : ModuleBase<SocketCommandContext>
{
[Command("start")]
public async Task StartMessageTimer()
{
}
public class MessageTimer
{
private static System.Timers.Timer messageTimer;
public static void StartTimer()
{
messageTimer = new System.Timers.Timer(30000);
messageTimer.Elapsed += OnTimerElapsed;
messageTimer.AutoReset = true;
messageTimer.Enabled = true;
}
public static void OnTimerElapsed(object source, ElapsedEventArgs e)
{
Console.WriteLine("Test Message");
}
}
}
}

如果我理解正确,您只需要使OnTimerElapsed方法异步:

public static async void OnTimerElapsed(...)

然后在MessageTimer类中创建一个全局变量SocketCommandContext _Context;,将属性SocketCommandContext context添加到StartTime方法中,并将_Context设置为context

然后打电话OnTimerElapsed方法中的await _Context.Channel.SendMessageAsync("Test Message");

public static Class MessageTimer
{
private static System.Timers.Timer messageTimer;
private static SocketCommandContext _Context;
public static void StartTimer(SocketCommandContext context)
{
_Context = context;
messageTimer = new System.Timers.Timer(30000);
messageTimer.Elapsed += OnTimerElapsed;
messageTimer.AutoReset = true;
messageTimer.Enabled = true;
}
public static void OnTimerElapsed(object source, ElapsedEventArgs e)
{
_Context.Channel.SendMessageAsync("Test Message");
Console.WriteLine("Test Message");
}
}

现在您只需要在启动命令中调用MessageTimer.StartTimer(Context);

public class Commands : ModuleBase<SocketCommandContext>
{
[Command("start")]
public async Task StartMessageTimer()
{
MessageTimer.StartTimer(Context);
}
}

我按照这个例子设置了我的自动消息系统。它仍然适用于Discord.NET的最新版本(截至本文发布之日,版本3.6.1(

using System;
using System.Threading; // 1) Add this namespace
using System.Threading.Tasks;
using Discord.Commands;
public class TimerService
{
private readonly Timer _timer; // 2) Add a field like this
// This example only concerns a single timer.
// If you would like to have multiple independant timers,
// you could use a collection such as List<Timer>,
// or even a Dictionary<string, Timer> to quickly get
// a specific Timer instance by name.
public TimerService(DiscordSocketClient client)
{
_timer = new Timer(async _ =>
{
// 3) Any code you want to periodically run goes here, for example:
var chan = client.GetChannel(knownId) as IMessageChannel;
if (chan != null)
await chan.SendMessageAsync("hi");
},
null,
TimeSpan.FromMinutes(10),  // 4) Time that message should fire after the timer is created
TimeSpan.FromMinutes(30)); // 5) Time after which message should repeat (use `Timeout.Infinite` for no repeat)
}
public void Stop() // 6) Example to make the timer stop running
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
public void Restart() // 7) Example to restart the timer
{
_timer.Change(TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(30));
}
}
public class TimerModule : ModuleBase
{
private readonly TimerService _service;
public TimerModule(TimerService service) // Make sure to configure your DI with your TimerService instance
{
_service = service;
}
// Example commands
[Command("stoptimer")]
public async Task StopCmd()
{
_service.Stop();
await ReplyAsync("Timer stopped.");
}
[Command("starttimer")]
public async Task RestartCmd()
{
_service.Restart();
await ReplyAsync("Timer (re)started.");
}
}

您需要在启动文件中设置依赖项注入才能正常工作。如果你不知道如何做到这一点,Discord.NET和Microsoft文档应该会让你知道该怎么做。

相关内容

最新更新