I created a Discord Bot where I can send text commands. For example, typing !start, would send the message that has the start parameter set. The bot works, I can send the commands, I'm just having a hard time figuring out how to set the timer and tie it to the command. Maybe setting it up as a command is the wrong way to go about it, but I can't figure out another way to send a message.
Here is my code so far. I've looked in several places online. Many resources say I need to include the System.Timer using, which I have. I'm just having a hard time figuring out how to tie the timer, the command itself, and which channel it goes into, all together. Any help would be appreciated.
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");
}
}
}
}
If I'm understanding it correctly you just need to make your
OnTimerElapsed
method async:public static async void OnTimerElapsed(...)
Then create a global variable
SocketCommandContext _Context;
in your MessageTimer Class, add the attributeSocketCommandContext context
to the StartTime method and set_Context
tocontext
And then Call
await _Context.Channel.SendMessageAsync("Test Message");
in yourOnTimerElapsed
method:Now you just need to call
MessageTimer.StartTimer(Context);
in your start command