public async Task DisableCommandFor(CommandContext context, string userName, [RemainingText] string commandName) { var guild = context.GetGuild(); var nameFragments = commandName.Split("."); var user = guild.FindMember(userName); if (user == null) { await context.RespondAsync($"Can not find the user '{userName}'."); return; } Command command = context.CommandsNext.FindCommand(commandName, out _); if (command == null) { await context.RespondAsync("Could not find specified command."); return; } string botMessage = null; if (command is CommandGroup group) { string subcommands = string.Join(", ", group.Children.Select(x => $"`{x.QualifiedName}`")); botMessage = $"Disable command group `{group.Name}`` for {user.Username}?\nIt has these sub commands: {subcommands}'."; } else { botMessage = $"Disable command `{command.QualifiedName}` for {user.Username}?"; } botMessage += $"\nThe user '{userName}' won't be able to use the command(s)."; await context.CreateConfirmation( botMessage, async() => { this._context.SetRestrictionForUser( guild.Id, user.Id, new CommandRestriction { CommandName = command.QualifiedName } ); await context.RespondAsync($"Disabled `{command.QualifiedName}` for {user.Username}?."); }, async() => { await context.RespondAsync("Action aborted. No settings changed."); } ); }
public async Task RemoveAdminRole(CommandContext context, ulong roleId) { var guild = context.GetGuild(); var role = guild.Roles[roleId]; if (!_context.IsAdminRole(guild.Id, roleId)) { await context.RespondAsync("The role with this ID does not have priviliges."); return; } await context.CreateConfirmation( $"Give bot administration capabilities to `{role.Name}`?", () => { this._context.RemoveAdminRole(guild.Id, roleId); context.RespondAsync($"The role `{(role != null ? role.Name : roleId.ToString())}` can no longer use `admin` commands."); } ); }
public async Task AddAdminRole(CommandContext context, ulong roleId) { var guild = context.GetGuild(); var role = guild.Roles[roleId]; if (role == null) { await context.RespondAsync("Could not find role with that ID."); return; } await context.CreateConfirmation( $"Give bot administration capabilities to `{role.Name}`?", () => { this._context.AddAdminRole(guild.Id, roleId); context.RespondAsync($"Users with the `{role.Name}` role can now use `admin` commands."); } ); }
public async Task EnableCommand(CommandContext context, [RemainingText] string commandName) { var guild = context.GetGuild(); var nameFragments = commandName.Split("."); Command command = context.CommandsNext.FindCommand(commandName, out _); if (command == null) { await context.RespondAsync("Could not find specified command."); return; } if (!this._context.IsCommandDisabled(guild.Id, command.QualifiedName)) { await context.RespondAsync("Command isn't disabled."); return; } string botMessage = null; if (command is CommandGroup group) { string subcommands = string.Join(", ", group.Children.Select(x => $"`{x.QualifiedName}`")); botMessage = $"Enable command group `{group.Name}`?\nIt has these sub commands: {subcommands}'."; } else { botMessage = $"Enable command `{command.Name}`?"; } botMessage += $"\nThe command(s) will be enabled for everyone on the server."; await context.CreateConfirmation( botMessage, () => { this._context.EnableCommand(guild.Id, command.QualifiedName); context.RespondAsync($"Command `{command.QualifiedName}` is now enabled."); } ); }
public async Task EnableCommandFor(CommandContext context, string userName, [RemainingText] string commandName) { var guild = context.GetGuild(); var nameFragments = commandName.Split("."); var user = guild.FindMember(userName); if (user == null) { await context.RespondAsync($"Can not find the user '{userName}'."); return; } CommandRestriction restriction = _context.GetUserRestriction(guild.Id, user.Id)?.FindCommandRestriction(commandName); if (restriction == null) { await context.RespondAsync("The user is already allowed to use this command."); return; } string botMessage = $"Re-enable command `{restriction.CommandName}` for {user.Username}?"; await context.CreateConfirmation( botMessage, async() => { this._context.RemoveRestrictionForUser( guild.Id, user.Id, restriction ); await context.RespondAsync($"Re-enabled `{restriction.CommandName}` for {user.Username}?."); }, async() => { await context.RespondAsync("Action aborted. No settings changed."); } ); }
public async Task CreateElection( CommandContext context, [Description("Title that is being elected.")] string title, [Description("Planned start of the election (UTC).")] DateTime start, [Description("Planned end of the election (UTC).")] DateTime end, [Description("List of candidates.")] params DiscordUser[] candidateNames ) { if (start <= DateTime.UtcNow) { await context.RespondAsync($"`start` must not be in the past."); return; } if (start > end) { await context.RespondAsync($"`start` must be before `end`."); return; } if (candidateNames.Distinct().Count() != candidateNames.Length) { await context.RespondAsync("You have duplicate names in the candidate list."); return; } var guild = context.GetGuild(); List <Candidate> candidates = new(); candidates = candidateNames.Select((item, index) => new Candidate { Option = char.ConvertFromUtf32(65 + index), UserId = item.Id, Username = item.Username + "#" + item.Discriminator, Votes = 0, }).ToList(); var election = new Election() { Candidates = candidates, Title = title, Start = start.ToUniversalTime(), End = end.ToUniversalTime(), }; _pollRepository.AddElection(guild.Id, election); await context.RespondAsync(embed : election.GetEmbed()); await context.CreateConfirmation( "Schedule the above election poll?", () => { this._pollRepository.CommitElection(context.GetGuild().Id, election.ID); context.RespondAsync("Election committed and scheduled."); }, () => { this._pollRepository.RemoveElection(context.GetGuild().Id, election.ID); context.RespondAsync("Draft withdrawn."); } ); }
public async Task VoteFor( CommandContext context, [Description("ID of the election you want to vote in")] ulong electionId, [Description("All candidates you approve. Either by their full username or their assigned letter.")] params string[] approvals ) { var guild = context.GetGuild(); var members = await guild.GetAllMembersAsync(); var voterStatus = _pollRepository.CanVote(guild.Id, electionId, context.User.Id); switch (voterStatus) { case VoteStatus.ElectionNotFound: { var activeElections = _pollRepository.GetCurrentElections(guild.Id); if (activeElections.Count > 0) { await context.RespondAsync( $"No election with ID `{electionId}`. Current elections: " + string.Join(", ", activeElections.Select(y => $"**{y.Title}** (ID: {y.ID})")) ); } else { await context.RespondAsync($"There are currently no elections to vote in."); } return; } case VoteStatus.ElectionNotStarted: { await context.RespondAsync($"Election #{electionId} has not started yet."); return; } case VoteStatus.ElectionOver: { await context.RespondAsync($"Election #{electionId} is already over and decided."); return; } case VoteStatus.AlreadyVoted: { await context.RespondAsync($"You already voted in this election. Unfortunately, you can not change your vote."); return; } } if (approvals.Distinct().Count() != approvals.Length) { await context.RespondAsync("You must not list any candidate more than once."); return; } var election = _pollRepository.GetElection(guild.Id, electionId); List <Candidate> candidates = new(); foreach (var item in approvals) { Candidate foundCandidate = null; if (item.Length == 1) { foundCandidate = election.Candidates.Find(y => y.Option.ToLower() == item.ToLower()); } if (foundCandidate == null) { foundCandidate = election.Candidates.Find(y => y.Username.ToLower() == item.ToLower()); if (foundCandidate == null) { var foundMemberId = guild.FindMember(item)?.Id; foundCandidate = election.Candidates.Find(y => y.UserId == foundMemberId); } } if (foundCandidate == null) { await context.RespondAsync($"Could not find `{item}` on the list of candidates."); return; } candidates.Add(foundCandidate); } var confirmationMessage = $"Please confirm that these are the correct candidate(s) you approve of for {election.Title} and that your ballot is complete?:\n\n" + $"{string.Join("\n", candidates.Select(y => " - <@" + y.UserId + ">"))}\n\n" + $"**Your vote can not be changed after confirmation. Make sure you selected all your approved candidates.**"; await context.CreateConfirmation( confirmationMessage, async() => { var(hashBefore, hashafter) = _pollRepository.SaveVotes(guild.Id, election.ID, context.User.Id, candidates, context.Client); await context.RespondAsync("Your vote has been cast. These hashes can be used to check the audit log after the election is over: \n" + $"Hash before: {hashBefore}\nHash after: {hashafter}"); } ); }