/// <summary>Method called when OnTrackStarted event is fired. /// </summary> private async Task OnTrackStarted(TrackStartEventArgs arg) { var textChannel = arg.Player.TextChannel; var track = arg.Track; /* Send "Now Playing" message to text channel, and delete it after the music ends * (this prevents bot spamming "Now playing" messages when queue is long) */ _interactivityService.DelayedSendMessageAndDeleteAsync(textChannel, deleteDelay: track.Duration, embed: await CustomFormats.NowPlayingEmbed(track)); }
/// <summary> /// Removes X(count) messages from chat. /// </summary> public async Task CleanMessagesAsync(int count, SocketCommandContext context) { //We only delete 100 messages at a time to prevent bot from getting overloaded if (count > 100) { await context.Channel.SendMessageAsync( embed : CustomFormats.CreateErrorEmbed(context.User.Mention + " You cannot delete more than 100 messages at once")); return; } /* Saves all messages user specified in a variable, next * those messages are deleted and a message is sent to the textChannel * saying that X messages were deleted <- this message is deleted 2.3s later */ //Save messages to delete in a variable var messages = await context.Channel.GetMessagesAsync(count + 1).FlattenAsync(); var messagesToDelete = messages.ToList(); if (messagesToDelete.Any(msg => DateTimeOffset.UtcNow - msg.CreatedAt.UtcDateTime > TimeSpan.FromDays(14))) { await context.Channel.SendMessageAsync( embed : CustomFormats.CreateErrorEmbed(context.User.Mention + " You cannot delete messages older than 2 weeks.")); return; } //Delete messages to delete await context.Guild.GetTextChannel(context.Channel.Id).DeleteMessagesAsync(messagesToDelete, new RequestOptions { AuditLogReason = $"{context.User} requested message cleaning" }); //Send success message that will disappear after 2300 milliseconds _interactivityService.DelayedSendMessageAndDeleteAsync(context.Channel, null, TimeSpan.FromMilliseconds(2300), null, false, CustomFormats.CreateBasicEmbed("Messages deleted", $":white_check_mark: Deleted **{count}** messages.", 0x268618)); }
/// <summary> Send an embed with info about specified command. </summary> public async Task HelpAsync(SocketCommandContext context, string commandName) { //Get guilds custom prefix. //Sets prefix to - if the guild doesn't have a custom prefix var prefix = _botContext.GetGuildPrefix(context.Guild.Id); //Search for commands equal to commandName var searchResult = _commandService.Search(context, commandName); //If no commands are found if (!searchResult.IsSuccess) { _interactivityService.DelayedSendMessageAndDeleteAsync(context.Channel, null, TimeSpan.FromSeconds(5), null, false, CustomFormats.CreateErrorEmbed($"**Unknown command: `{commandName}`**")); return; } //If there is a match, then get the command which matches the command specified var cmd = searchResult.Commands.First(x => x.Alias.Contains(commandName)).Command; //Check if user has permission to execute said command var canExecute = await cmd.HasPermissionToExecute(context, _serviceProvider); if (!canExecute) { await context.Channel.SendMessageAsync( embed : CustomFormats.CreateErrorEmbed("You don't have permission for that command!")); return; } //Get command parameters var parameters = cmd.Parameters.ToArray(); //True if the command has parameters, false if the command doesn't var hasParameters = parameters.Any(); var usageBuilder = new StringBuilder(); //If the command has parameters, we build a usage string if (hasParameters) { foreach (var parameter in parameters) { //If the current parameter is optional, then we append <parameter> so the user knows it is optional if (parameter.IsOptional) { usageBuilder.Append($"<{parameter.Name}> "); continue; } //If it isnt optional, we append [parameter] so the user knows that the parameter is required usageBuilder.Append($"[{parameter.Name}] "); } //Removes the last char of the string, because when building the string a blank space is always left at the end usageBuilder.Remove(usageBuilder.Length - 1, 1); } //Send message on how to use the command var helpEmbed = new EmbedBuilder() .WithColor(0x268618) .WithTitle($"Command: {cmd.Aliases[0]}") .WithDescription(hasParameters //If command has parameters ? $"**Aliases:** `{string.Join(", ", cmd.Aliases)}`\n" + $"**Description:** {cmd.Summary}\n" + $"**Usage:** `{prefix}{cmd.Aliases[0]} {usageBuilder}`" //If command doesn't have parameters : $"**Aliases:** `{string.Join(", ", cmd.Aliases)}`\n" + $"**Description:** {cmd.Summary}\n" + $"**Usage:** `{prefix}{cmd.Aliases[0]}`") .WithFooter(hasParameters ? "Parameters inside <angle brackets> are optional." : ""); await context.Channel.SendMessageAsync(embed : helpEmbed.Build()); }