/*the main method*/ public async Task MainAsync() { config = GetConfiguration(); //construct the "bot" client = new DiscordSocketClient(); var commands = new CommandService(); var services = new ServiceCollection() .AddSingleton(client) .AddSingleton(commands) .AddSingleton(config) .AddSingleton <ConnectionService>() .AddSingleton <CommandHandlingService>() .AddSingleton <LoggingService>() .BuildServiceProvider(); //initialize the logging service services.GetRequiredService <LoggingService>(); //connect to discord await services.GetRequiredService <ConnectionService>().StartAsync(); //initialize the command handling service services.GetRequiredService <CommandHandlingService>(); //sort the RemindMe file initially RemindMeService.SortTextFile(); //start the timer that looks at the file for reminders Timer timer = new Timer(CheckIfNeedReminded, "some state", TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); //block this task until the program is closed await Task.Delay(-1); }
private void CheckIfNeedReminded(object state) { ulong guildId = UInt64.Parse(config["cryptoServer"]); SocketGuild guild = client.GetGuild(guildId); string message = RemindMeService.CheckTime(guild); if (message != "") { ulong channelId = UInt64.Parse(config["cryptoChannel"]); SocketTextChannel channel = guild.GetTextChannel(channelId); channel.SendMessageAsync(message); } }
public async Task RunBotAsync() { string botToken = ""; using (var context = new SqliteDbContext()) { // Migrate the database //await context.Database.EnsureCreatedAsync(); await context.Database.MigrateAsync(); //... the remaining code } _client = new DiscordSocketClient(new DiscordSocketConfig() { MessageCacheSize = 20 }); _commands = new CommandService(); _remindMeService = new RemindMeService(_client); _services = new ServiceCollection() .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton(_remindMeService) .BuildServiceProvider(); _client.Log += Log; _client.Ready += Ready; await RegisterCommandsAsync(); using (var Stream = new FileStream(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace(@"bin\Debug\netcoreapp2.1", @"Data\Token.txt"), FileMode.Open, FileAccess.Read)) using (var ReadToken = new StreamReader(Stream)) { botToken = ReadToken.ReadToEnd(); } await _client.LoginAsync(Discord.TokenType.Bot, botToken); await _client.StartAsync(); await Task.Delay(-1); }
public async Task RemindMe(params string[] stringArray) { if (stringArray.Length < 1) { await ReplyAsync("Please add a time duration."); } else if (stringArray.Length < 2) { await ReplyAsync("Please add a message."); } else if (stringArray.Length > 2) { await ReplyAsync("You have too many parameters. Please surround the message in quotes"); } else { string user = Context.User.Username; DateTime currentTime = this.Context.Message.Timestamp.DateTime; RemindMeService.InputIntoDB(stringArray, currentTime, user); await ReplyAsync("I will remind you when the time comes."); } }
protected RemindMeModule(RemindMeService service) { _service = service; }
protected RemindMeModuleImplementation(RemindMeService service) : base(service) { }