public static async Task <PhotoConfig> Load() { var json = ""; try { json = await File.ReadAllTextAsync("config.json"); } catch (IOException e) { Console.WriteLine("Could not read configuration! Creating new config file."); } var photoConfig = new PhotoConfig(); try { photoConfig = JsonSerializer.Deserialize <PhotoConfig>(json); } catch (JsonException e) { Console.WriteLine("Could not read configuration! Creating new config file."); } return(photoConfig); }
async Task OnPhotoVoteReceivedAsync(Cacheable <IUserMessage, ulong> cacheable, ISocketMessageChannel channel, SocketReaction reaction) { var message = await cacheable.DownloadAsync(); if (!message.Author.IsPhotoUser()) { return; } var socketChannel = SocketGuild.GetTextChannel(channel.Id); if (socketChannel.Category.Id != Config.PhotoCategoryId) { return; } var photoMessage = Config.Photos.Find(element => element.MessageId == message.Id); if (photoMessage == null) { return; } if (reaction.Emote.IsUpvote()) { photoMessage.Upvotes += 1; } if (reaction.Emote.IsDownvote()) { photoMessage.Downvotes += 1; } await PhotoConfig.SaveAsync(); }
static async Task MainAsync(string[] args) { var token = await File.ReadAllTextAsync("token.txt"); var config = await PhotoConfig.Load(); var photoBot = new PhotoBot(config); await photoBot.ConnectAsync(token); await PhotoConfig.SaveAsync(); await Task.Delay(-1); }
public PhotoBot(PhotoConfig config) { Service.PhotoBot = this; Config = config; SocketClient = new DiscordSocketClient(); RestClient = new DiscordRestClient(); Commands = new CommandService(); Parser = new PhotoBotParser(SocketClient, Commands); SocketClient.Log += OnLogAsync; SocketClient.Connected += OnSocketConnectedAsync; SocketClient.MessageReceived += OnMessageReceivedAsync; SocketClient.ReactionAdded += OnReactionAddedAsync; SocketClient.ReactionRemoved += OnReactionRemovedAsync; }
async Task OnProposalVoteReceivedAsync(Cacheable <IUserMessage, ulong> cacheable, ISocketMessageChannel channel, SocketReaction reaction) { var message = await cacheable.DownloadAsync(); if (!message.Author.IsPhotoUser()) { return; } var socketChannel = SocketGuild.GetTextChannel(channel.Id); if (socketChannel.Category.Id != Config.PhotoCategoryId) { return; } var proposal = Config.Proposals.Find(element => element.UserId == message.Author.Id); if (proposal == null) { return; } if (reaction.Emote.IsUpvote()) { proposal.Upvotes += 1; } if (reaction.Emote.IsDownvote()) { proposal.Downvotes += 1; } if (reaction.Emote.IsCancel()) { if (reaction.User.Value.Id == message.Author.Id) { var socketUser = SocketGuild.GetUser(reaction.User.Value.Id); await DeleteProposalAsync(socketUser); } } await PhotoConfig.SaveAsync(); }
public async Task GetPhotoUsersAsync() { await SocketGuild.DownloadUsersAsync(); var users = SocketGuild.Users; Config.PhotoUserIds = new List <ulong>(); foreach (var user in users) { if (!user.IsPhotoUser()) { continue; } Config.PhotoUserIds.Add(user.Id); } await PhotoConfig.SaveAsync(); }
public async Task DeleteProposalAsync(SocketGuildUser user) { var proposal = Config.Proposals.Find(element => element.UserId == user.Id); if (proposal == null) { return; } var proposalsChannel = SocketGuild.GetTextChannel(Config.CurrentProposalsChannelId); try { await proposalsChannel.DeleteMessageAsync(proposal.MessageId); } catch (ArgumentException e) { } Config.Proposals.RemoveAll(element => element.UserId == user.Id); await PhotoConfig.SaveAsync(); }
async Task OnProposalReceivedAsync(SocketMessage message) { var userId = message.Author.Id; if (!Config.PhotoUserIds.Contains(userId)) { await message.DeleteAsync(); return; } Config.Proposals ??= new List <PhotoMessage>(); if (Config.Proposals.Any(element => element.UserId == userId)) { await message.DeleteAsync(); return; } var proposal = new PhotoMessage { MessageId = message.Id, UserId = userId, Topic = message.Content, ImageUrl = message.Attachments.ElementAt(0).Url, }; Config.Proposals.Add(proposal); Console.WriteLine($"Added new proposal from user {proposal.UserId} with topic {proposal.Topic} and image {proposal.ImageUrl}"); await message.AddReactionAsync(new Emoji("✅")); await message.AddReactionAsync(new Emoji("❌")); await message.AddReactionAsync(new Emoji("🚫")); await PhotoConfig.SaveAsync(); }
async Task OnPhotoReceivedAsync(SocketMessage message) { Config.Photos ??= new List <PhotoMessage>(); if (Config.Photos.Any(element => element.UserId == message.Author.Id)) { await message.DeleteAsync(); return; } var photoMessage = new PhotoMessage { UserId = message.Author.Id, MessageId = message.Id, ImageUrl = message.Attachments.First().Url, }; Config.Photos.Add(photoMessage); await PhotoConfig.SaveAsync(); }