public async Task <Unit> Handle(RegisterDiscordBotCommand request, CancellationToken cancellationToken) { using (var botLock = await _lockFactory.CreateLockAsync("rspeer_discord_bot_lock", TimeSpan.FromMinutes(30))) { if (!botLock.IsAcquired) { return(Unit.Value); } _client = await _provider.Get(); _client.MessageReceived += OnMessage; _client.Disconnected += OnDisconnect; } return(Unit.Value); }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { var message = notification.Content; if (notification.IsBot) { return; } if (message != "!stats") { return; } if (!notification.InGuild) { return; } var total = await _redis.Get <long>("connected_client_count"); var toSend = $"There are currently {total} clients online."; var client = await _provider.Get(); var task = client?.GetGuild(notification.GuildId)?.GetTextChannel(notification.ChannelId)?.SendMessageAsync(toSend); if (task != null) { await task; } }
public async Task Execute() { var client = await _provider.Get(); await CheckExpiredInstances(client); await CheckExpiredScripts(client); await CheckExpiredInuvation(client); }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { if (notification.IsBot) { return; } if (!notification.Content.StartsWith("!")) { return; } var command = notification.Content?.Split(" ")[0]?.Substring(1); if (command == null) { return; } var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId)? .GetTextChannel(notification.ChannelId); if (channel == null) { return; } if (command == "h") { await SendList(channel); return; } var key = $"discord:bot:reply:{command}"; var exists = await _redis.GetString(key); if (exists != null) { await channel.SendMessageAsync(exists); return; } var db = await _db.SiteConfig.FirstOrDefaultAsync(w => w.Key == key, cancellationToken); if (db != null) { await _redis.Set(key, db.Value, TimeSpan.FromHours(1)); await channel.SendMessageAsync(db.Value); } }
private async Task SendMessage(DiscordMessageEvent notification, string message) { var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId)? .GetTextChannel(notification.ChannelId); if (channel == null) { return; } await channel.SendMessageAsync(message); }
public async Task <bool> Handle(SendDiscordPrivateMessageCommand request, CancellationToken cancellationToken) { var client = request.Client ?? await _provider.Get(); var guild = client.GetGuild(_guild); if (guild == null) { return(false); } var user = await _db.DiscordAccounts.Where(w => w.UserId == request.UserId).FirstOrDefaultAsync(cancellationToken); if (user == null) { return(false); } var discordUser = guild.GetUser(Convert.ToUInt64(user.DiscordUserId)); if (discordUser == null) { return(false); } try { var sent = await discordUser.SendMessageAsync(request.Message); if (sent != null && sent.Id != default && request.SendDisclaimer) { await discordUser.SendMessageAsync( "Don't want to receive alerts for expiring orders? Reply back with: " + "!disable_alerts\nTo enable alerts, reply with !enable_alerts"); } return(sent != null && sent.Id != default); } catch (Exception) { return(false); } }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { var message = notification.Content; if (!notification.IsDirector) { return; } if (notification.Channel == null || !notification.Content.StartsWith("!status")) { return; } var split = message.Split(" "); var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId).GetTextChannel(notification.ChannelId); if (channel == null) { return; } if (split.Length < 3) { await channel.SendMessageAsync( "Invalid command, format must be: !status MESSAGE (true || false)"); return; } var content = split.Take(split.Length - 1).Join(" ").Replace("!status", string.Empty).Trim(); var toShow = split[split.Length - 1].Trim(); if (string.IsNullOrEmpty(content)) { await channel.SendMessageAsync( "You must specify a message to show. If wanting to remove the message, just put false as the third parameter. Example: !status no message false"); return; } if (!bool.TryParse(toShow, out var shouldShow)) { await channel.SendMessageAsync("Third parameter must be true or false."); return; } var shouldShowConfig = await _db.SiteConfig.FirstOrDefaultAsync(w => w.Key == "status:message:enabled", cancellationToken : cancellationToken); var messageConfig = await _db.SiteConfig.FirstOrDefaultAsync(w => w.Key == "status:message", cancellationToken : cancellationToken); if (shouldShowConfig == null || messageConfig == null) { await channel.SendMessageAsync( "Failed to find config by key status:message:enabled || status:message"); return; } shouldShowConfig.Value = shouldShow ? bool.TrueString : bool.FalseString; messageConfig.Value = content; _db.SiteConfig.Update(shouldShowConfig); _db.SiteConfig.Update(messageConfig); await _db.SaveChangesAsync(cancellationToken); await channel.SendMessageAsync( $"Successfully updated status message to {content} and should show to {shouldShow}."); }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { var message = notification.Content; if (!message.StartsWith("!verify") || !message.Contains("!verify")) { return; } var isPrivateMessage = notification.Channel == $"@{notification.Username}#{notification.Discriminator}"; if (notification.IsBot) { return; } if (notification.Channel != "discord-verification-bot" && !isPrivateMessage) { return; } var enabled = await _mediator.Send(new IsReadOnlyModeQuery(), cancellationToken); var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId)?.GetTextChannel(notification.ChannelId); if (!isPrivateMessage && channel == null) { return; } if (channel != null && enabled) { await channel.SendMessageAsync("RSPeer is in read only due to maintenance, services will be restored shortly. For more information, check status on https://app.rspeer.org"); return; } if (!isPrivateMessage && message != "!verify") { if (!notification.IsDirector) { await channel.DeleteMessageAsync(notification.Id); } return; } var config = await _mediator.Send(new GetSiteConfigOrThrowCommand { Key = "discord:bot:verification:enabled" }, cancellationToken); if (!bool.Parse(config)) { return; } if (!isPrivateMessage && message == "!verify") { var instructions = await _mediator.Send(new GetSiteConfigOrThrowCommand { Key = "discord:verification:message" }, cancellationToken); await client.GetUser(notification.UserId).SendMessageAsync(instructions); return; } await HandlePrivateMessage(client, notification, cancellationToken); }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { if (!notification.IsDirector) { return; } if (!notification.Content.StartsWith("!modscript")) { return; } var split = notification.Content.Split(" "); var client = await _provider.Get(); if (split.Length < 2) { await client.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId) .SendMessageAsync("Commands: clear, clear HASH_HERE, list"); return; } var command = split[1]; try { if (command == "clear") { if (split.Length == 3) { var hash = split[2].Trim(); _db.Files.RemoveRange(_db.Files.Where(w => w.Name == $"inuvation_modscript_{hash}")); await _db.SaveChangesAsync(cancellationToken); await client.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId) .SendMessageAsync($"Successfully removed inuvation modscript {hash}."); } else { _db.Files.RemoveRange(_db.Files.Where(w => w.Name.StartsWith("inuvation_modscript_"))); await _db.SaveChangesAsync(cancellationToken); await client.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId) .SendMessageAsync("Successfully removed inuvation modscripts."); } } if (command == "list") { var names = await _db.Files.Where(w => w.Name.StartsWith("inuvation_modscript_")).Select(w => w.Name).ToListAsync(); var formatted = names.Count == 0 ? "No modscripts have been saved." : string.Join(',', names); await client.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId) .SendMessageAsync(formatted); } } catch (Exception e) { await client.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId) .SendMessageAsync(e.Message); } }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { var message = notification.Content; if (!notification.IsTokenSeller) { return; } if (notification.Channel == null || notification.Channel != "tokens" || !notification.Content.StartsWith("!tokens")) { return; } var enabled = await _mediator.Send(new IsReadOnlyModeQuery(), cancellationToken); var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId).GetTextChannel(notification.ChannelId); if (channel == null) { return; } if (enabled) { await channel.SendMessageAsync("RSPeer is in read only mode due to maintenance, services will be restored shortly. For more information, check status on https://app.rspeer.org"); return; } var split = message.Split(" "); if (split.Length != 3) { await channel.SendMessageAsync("Invalid command, format must be: !tokens email amount"); return; } var email = split[1]; var amount = split[2]; if (!int.TryParse(amount, out var quantity) || quantity <= 0 || quantity >= 1000000) { await channel.SendMessageAsync("Invalid quantity. Must be greater than 0 and less than 1 million."); return; } var user = await _mediator.Send(new GetUserByEmailQuery { Email = email }, cancellationToken); if (user == null) { await channel.SendMessageAsync("Failed to find user by that email address."); return; } var item = await _mediator.Send(new GetItemBySkuQuery { Sku = "tokens" }, cancellationToken); using (var transaction = await _db.Database.BeginTransactionAsync(cancellationToken)) { var order = new Order { ItemId = item.Id, Quantity = quantity, UserId = user.Id, Timestamp = DateTimeOffset.UtcNow, Status = OrderStatus.Completed, Total = quantity * item.Price }; await _db.Orders.AddAsync(order, cancellationToken); await _db.SaveChangesAsync(cancellationToken); await _mediator.Send(new UserUpdateBalanceCommand { Reason = $"token:sale:{notification.Username}", Type = AddRemove.Add, UserId = user.Id, OrderId = order.Id, Amount = quantity }, cancellationToken); transaction.Commit(); } user = await _mediator.Send(new GetUserByIdQuery { AllowCached = false, Id = user.Id }, cancellationToken); await channel.SendMessageAsync($"Successfully updated {user.Username} ({user.Email})'s balance by {quantity}. New balance is {user.Balance} tokens."); }
public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken) { if (!notification.IsDirector || !notification.Content.StartsWith("!")) { return; } var client = await _provider.Get(); var channel = client?.GetGuild(notification.GuildId) .GetTextChannel(notification.ChannelId); var split = notification.Content.Split(" "); if (split.Length < 3) { return; } var command = split[0]; var key = split[1]; var value = notification.Content .Substring(command.Length + key.Length + 2).Trim(); var exists = await _db.SiteConfig.FirstOrDefaultAsync(w => w.Key == $"discord:bot:reply:{key}", cancellationToken : cancellationToken); if (command == "!add_reply") { if (exists != null) { await channel.SendMessageAsync($"Reponse by {key} already exists. If you wish to change, please delete with !remove_reply"); return; } await _db.SiteConfig.AddAsync(new Domain.Entities.SiteConfig { Key = $"discord:bot:reply:{key}", Value = value }, cancellationToken); await _redis.Remove("discord:bot:replies"); await _db.SaveChangesAsync(cancellationToken); await channel.SendMessageAsync($"Successfully added response {key}."); } if (command == "!remove_reply") { if (exists != null) { _db.Remove(exists); await _db.SaveChangesAsync(cancellationToken); var redisKey = $"discord:bot:reply:{key}"; await _redis.Remove(redisKey); await _redis.Remove("discord:bot:replies"); await channel.SendMessageAsync($"Successfully removed {key}."); } } }