Discord Bot - 计时器

时间:2018-01-17 17:43:09

标签: timer discord

我为我的机器人编写了一个代表系统,每次用户代表某人时我都会尝试添加冷却时间,而且,它确实没有用...

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Windows">
      <HintPath>..\..\..\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd</HintPath>
      <IsWinMDFile>true</IsWinMDFile>
    </Reference>
  </ItemGroup>

</Project>

只有我可以将帐号传入OnTimedEvent,然后我才能轻松解决这个问题,但无论我尝试什么,我都无法将其传递给该函数......任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:0)

有可能使用Lambda匿名方法我不确定这是否实际上是正确的但有些类似它应该是这样的。

您还可以在此处阅读如何执行此操作How do I pass an object into a timer event?

private static void SetTimer(SocketUser user) {
 var account = UserAccounts.GetAccount(user);
 account.TimerOn = true;

 timer = new System.Timers.Timer(1000); //(1000 * 60) * 10
 timer.Elapsed += e => {
  Console.WriteLine("Timer ended {0} for {1}", e.SignalTime, account.Whatever);
 };
 timer.Enabled = true;
 timer.AutoReset = false;
}

private static void SetTimer(SocketUser user) {
 var account = UserAccounts.GetAccount(user);
 account.TimerOn = true;

 timer = new System.Timers.Timer(1000); //(1000 * 60) * 10
 timer.Elapsed += e => {
  Console.WriteLine("Timer ended {0} for {1}", e.SignalTime, account.Whatever);
DoSomething(account);
 };
 timer.Enabled = true;
 timer.AutoReset = false;
}
void DoSomething(Account account){
//ToDo
}