public async Task EnableNowPlaying(CommandContext ctx) { // Get current channel where command is used var currentChannel = enabledChannels.FirstOrDefault(p => p.discordChannel.Id == ctx.Channel.Id); // If this channel doesn't exist add it if (currentChannel == null) { currentChannel = new EnabledChannel(ctx.Channel); enabledChannels.Add(currentChannel); await ctx.RespondAsync("I will inform on this channel about the current songs playing every 15 seconds."); // If we don't have current song, don't post info about it if (currentAnisonPlayingSong != string.Empty) { RepostSongInfo(currentChannel); } } else { await ctx.RespondAsync("I'm already posting info on this channel."); } }
private async void RepostSongInfo(EnabledChannel channel) { // If last message exist if (channel.lastMessage != null) { // Try find last message try { var message = await channel.discordChannel.GetMessagesAsync(1); // If last message on channel is the last message posted by bot, edit it if (message.FirstOrDefault() == channel.lastMessage) { try { await channel.lastMessage.ModifyAsync("", CreateEmbedWithSongData()); return; } catch (Exception ie) { // Something went wrong Console.WriteLine("Error: Edit last message failed."); Console.WriteLine("Exception: " + ie.Message); Console.WriteLine("Inner Exception: " + ie?.InnerException?.Message); Console.WriteLine("Stack trace: " + ie.StackTrace); } } } catch (Exception ie) { // Something went wrong Console.WriteLine("Error: Get last message from channel failed."); Console.WriteLine("Exception: " + ie.Message); Console.WriteLine("Inner Exception: " + ie?.InnerException?.Message); Console.WriteLine("Stack trace: " + ie.StackTrace); } } // Try delete last message try { if (channel.lastMessage != null) { await channel.discordChannel.DeleteMessageAsync(channel.lastMessage); } } catch (Exception ie) { //Bot couldn't find message. Maybe someone deleted it. Console.WriteLine("Error: Delete bot's last message failed. Message might be already deleted by someone else."); Console.WriteLine("Exception: " + ie.Message); Console.WriteLine("Inner Exception: " + ie?.InnerException?.Message); Console.WriteLine("Stack trace: " + ie.StackTrace); } try { channel.lastMessage = await channel.discordChannel.SendMessageAsync("", false, CreateEmbedWithSongData()); } catch (Exception ie) { Console.WriteLine("Error: Send music info failed."); Console.WriteLine("Exception: " + ie.Message); Console.WriteLine("Inner Exception: " + ie?.InnerException?.Message); Console.WriteLine("Stack trace: " + ie.StackTrace); } }