public InteractiveService(WumpusBotClient client, TimeSpan?defaultTimeout = null) { Client = client; Client.Gateway.MessageReactionAdd += HandleReaction; _callbacks = new Dictionary <ulong, IReactionCallback>(); _defaultTimeout = defaultTimeout ?? TimeSpan.FromSeconds(15); }
private async Task MainAsync() { var manager = new LogManager(LogSeverity.Verbose); var client = new WumpusBotClient(restRateLimiter: new DefaultRateLimiter(), logManager: manager) { Authorization = new AuthenticationHeaderValue("Bot", Environment.GetEnvironmentVariable("Espeon")), }; manager.Output += message => Console.WriteLine(message); var commands = new CommandServiceBuilder <EspeonContext>() .AddPipeline((ctx, next) => { if (ctx.Context.Message.StartsWith("!")) { ctx.PrefixLength = 1; } return(next.Invoke()); }) .AddCommandParser <DefaultCommandParser>() .AddModule <Commands>() .BuildCommandService(); var services = new ServiceCollection() .AddSingleton <InteractiveService>() .AddSingleton(client) .AddSingleton(commands) .BuildServiceProvider(); _ = Task.Run(async() => { await client.RunAsync(); }); try { client.Gateway.MessageCreate += async message => { if (message.Author.Bot.IsSpecified) { return; } if (!message.Content.StartsWith(new Utf8String("!"))) { return; } var channel = await client.Rest.GetChannelAsync(message.ChannelId); var context = new EspeonContext(client.Rest, message, channel, commands); await commands.ExecuteAsync(context, services); }; } catch (Exception e) { Console.WriteLine(e); } await Task.Delay(-1); }
public DiscordHandler(IConfiguration config) { _config = config; _serializer = new DiscordJsonSerializer(); _logManager = new DiscordLogManager(LogSeverity.Verbose); _hub = new HubConnectionBuilder() .WithUrl(Path.Combine(_config["url"], _config["discord:hub_url"])) .Build(); var token = _config["discord:token"]; if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentNullException("Discord token is missing in the configuration file"); } _bot = new WumpusBotClient(logManager: _logManager); _bot.Authorization = new AuthenticationHeaderValue("Authorization", "Bot " + _config["discord:token"]); _bot.Gateway.ReceivedPayload += OnPayloadReceived; }