protected async Task UploadDatabaseBackupAsync(IMessageChannel channel, string databaseFilePath) { bool backupInProgress = GetDatabaseStatus(databaseFilePath).BackupInProgress; if (backupInProgress) { await DiscordUtilities.ReplyErrorAsync(channel, "A backup is already in progress. Please wait until it has completed."); } else { GetDatabaseStatus(databaseFilePath).BackupInProgress = true; if (System.IO.File.Exists(databaseFilePath)) { try { await DiscordUtilities.ReplyInfoAsync(channel, string.Format("Uploading database backup ({0:0.##} MB).\nThe backup will be posted in this channel when it is complete.", new System.IO.FileInfo(databaseFilePath).Length / 1024000.0)); await channel.SendFileAsync(databaseFilePath, string.Format("`Database backup ({0})`", DateUtilities.GetCurrentDateUtc())); } catch (Exception) { await DiscordUtilities.ReplyErrorAsync(channel, "Database file cannot be accessed."); } } else { await DiscordUtilities.ReplyErrorAsync(channel, "Database file does not exist at the specified path."); } GetDatabaseStatus(databaseFilePath).BackupInProgress = false; } }
public static async Task <bool> ReplyValidateZoneAsync(this OfcModuleBase moduleBase, IZone zone, string zoneName = "") { if (!zone.IsValid()) { string message = "No such zone exists."; if (!string.IsNullOrEmpty(zoneName)) { zoneName = ZoneUtilities.GetFullName(zoneName); if (zoneName.StartsWith("zone", System.StringComparison.OrdinalIgnoreCase)) { message = $"{zoneName.ToTitle().ToBold()} does not exist."; } else { message = $"Zone {zoneName.ToTitle().ToBold()} does not exist."; } } await DiscordUtilities.ReplyErrorAsync(moduleBase.Context.Channel, message); return(false); } return(true); }
public static async Task <bool> ReplyValidateZoneAsync(ICommandContext context, Common.Zones.IZone zone, string zoneName = "") { if (!zone.IsValid()) { string message = "No such zone exists."; if (!string.IsNullOrEmpty(zoneName)) { message = $"Zone {zoneName.ToTitle().ToBold()} does not exist."; } await DiscordUtilities.ReplyErrorAsync(context.Channel, message); return(false); } return(true); }
// Protected members protected override async Task OnMessageReceivedAsync(SocketMessage rawMessage) { ulong userId = rawMessage.Author.Id; // If the user has been banned, show an error message. // Bot admins cannot be banned. bool userIsBanned = botConfiguration.BannedUserIds?.Any(id => id.Equals(userId)) ?? false; bool userIsBotAdmin = botConfiguration.BotAdminUserIds?.Any(id => id.Equals(userId)) ?? false; if (userIsBanned && !userIsBotAdmin) { await DiscordUtilities.ReplyErrorAsync(rawMessage.Channel, "You do not have permission to use this command."); } else { await base.OnMessageReceivedAsync(rawMessage); } }
private async Task ShowGenericCommandErrorAsync(Optional <CommandInfo> command, ICommandContext context, IResult result) { if (result is null) { // Show a generic message if we don't have a result indicating what happened. if (command.IsSpecified) { await DiscordUtilities.ReplyErrorAsync(context.Channel, $"Something went wrong while executing the **{command.Value.Name}** command."); } else { await DiscordUtilities.ReplyErrorAsync(context.Channel, $"Something went wrong while executing the command."); } } else if (result is ExecuteResult executeResult && executeResult.Exception?.InnerException != null && !string.IsNullOrWhiteSpace(executeResult.Exception.InnerException.Message)) { await DiscordUtilities.ReplyErrorAsync(context.Channel, executeResult.Exception.InnerException.Message); }
public async Task Set(string key, string value) { if (Config.SetProperty(key, value)) { await DiscordUtilities.ReplySuccessAsync(Context.Channel, string.Format("Successfully set **{0}** to **{1}**.", key, value)); } else { await DiscordUtilities.ReplyErrorAsync(Context.Channel, string.Format("No setting with the name **{0}** exists.", key)); } // Reload commands (the commands available are dependent on configuration settings). await CommandService.InstallCommandsAsync(); // Update the bot's "Playing" status. await DiscordClient.SetGameAsync(Config.Playing); }
protected async Task ShowCommandErrorAsync(Optional <CommandInfo> command, ICommandContext context, IResult result) { if (result is null) { await ShowGenericCommandErrorAsync(command, context, result); } else if (result.Error == CommandError.BadArgCount) { // Get the name of the command that the user attempted to use. string commandName = GetCommandName(context.Message); // If help documentation exists for this command, display it. ICommandHelpInfo commandHelpInfo = await helpService.GetCommandHelpInfoAsync(commandName); if (commandHelpInfo != null) { EmbedBuilder embed = new EmbedBuilder(); embed.WithColor(Color.Red); embed.WithTitle(string.Format("Incorrect use of \"{0}\" command", commandName.ToLower())); embed.WithDescription("❌ " + result.ErrorReason); if (commandHelpInfo.Examples.Any()) { embed.AddField("Example(s) of correct usage:", string.Join(Environment.NewLine, commandHelpInfo.Examples .Select(e => string.Format("`{0}{1}{2}`", configuration.Prefix, commandName, e.SkipWords(1))))); } await context.Channel.SendMessageAsync("", false, embed.Build()); } else { await ShowGenericCommandErrorAsync(command, context, result); } } else if (result.Error == CommandError.UnknownCommand) { // Suggest the most-similar command as a possible misspelling. string messageContent = context.Message.Content.Substring(GetCommmandArgumentsStartIndex(context.Message)); string commandName = messageContent.GetFirstWord(); if (!string.IsNullOrEmpty(commandName)) { string suggestedCommandName = StringUtilities.GetBestMatch(commandName, GetCommandNames().Where(name => helpService.IsCommandAvailableAsync(context, name).Result)); ICommandHelpInfo commandHelpInfo = await helpService.GetCommandHelpInfoAsync(suggestedCommandName); await DiscordUtilities.ReplyErrorAsync(context.Channel, string.Format($"Unknown command. Did you mean {commandHelpInfo.Name.ToBold()}?")); } else { await ShowGenericCommandErrorAsync(command, context, result); } } else { await ShowGenericCommandErrorAsync(command, context, result); } }
public async Task ReplyErrorAsync(string message) => await DiscordUtilities.ReplyErrorAsync(Context.Channel, message);