private async Task <bool> VerifyUserPermissionsAsync(SocketCommandContext context, IVoiceChannel channel, ulong userID, ChannelPermission permission, CancellationToken cancellationToken) { IGuildUser user = await channel.Guild.GetUserAsync(userID, CacheMode.AllowDownload, new RequestOptions { CancelToken = cancellationToken }).ConfigureAwait(false); if (user.GuildPermissions.Administrator) { return(true); } bool isSelf = userID == context.User.Id; string memberName = isSelf ? "I" : "You"; if (!user.GetPermissions(channel).Has(ChannelPermission.ViewChannel | ChannelPermission.Connect)) { await context.InlineReplyAsync($"{this._einherjiOptions.CurrentValue.FailureSymbol} {memberName} don't have access to {GetVoiceChannelMention(channel)}.", cancellationToken).ConfigureAwait(false); return(false); } if (!user.GetPermissions(channel).Has(permission)) { await context.InlineReplyAsync($"{this._einherjiOptions.CurrentValue.FailureSymbol} {memberName} don't have *{GetPermissionDisplayName(permission)}* permission in {GetVoiceChannelMention(channel)}.", cancellationToken).ConfigureAwait(false); return(false); } return(true); }
private Task CmdCommandsAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { IOrderedEnumerable <IGrouping <string, CommandDescriptor> > commands = this.GetCommandDescriptors(context); if (commands.Any()) { EmbedBuilder embed = this.StartEmbed(context); string prefix = this.GetPrefix(context); StringBuilder commandsList = new StringBuilder(); foreach (IGrouping <string, CommandDescriptor> group in commands) { commandsList.Clear(); foreach (CommandDescriptor cmd in group) { commandsList.Append($"***{prefix}{cmd.DisplayName}***: {cmd.Summary}\n"); } embed.AddField(group.Key, commandsList.ToString(), inline: false); } return(context.ReplyAsync(null, false, embed.Build(), cancellationToken)); } else { return(context.InlineReplyAsync($"{_einherjiOptions.FailureSymbol} Ooops, I detected no commands... this obviously isn't right. Please let {GetAuthorText(context)} know!", cancellationToken)); } }
private async Task <SocketTextChannel> VerifyGuildChannelAsync(SocketCommandContext context, CancellationToken cancellationToken) { if (!(context.Channel is SocketTextChannel channel)) { await context.InlineReplyAsync($"{this._einherjiOptions.CurrentValue.FailureSymbol} Sir, this command is only applicable in guild channels.", cancellationToken).ConfigureAwait(false); return(null); } return(channel); }
private async Task <VoiceChannelMatch> MatchChannelAsync(SocketCommandContext context, Match match, int groupIndex, CancellationToken cancellationToken) { SocketGuildUser user = context.Guild.GetUser(context.User.Id); SocketVoiceChannel targetChannel; if (match.Groups.Count >= groupIndex + 1 && match.Groups[groupIndex].Success) { targetChannel = await this.VerifyValidVoiceChannelAsync(match.Groups[groupIndex], context.Guild, context.Channel, cancellationToken).ConfigureAwait(false); } else { targetChannel = user.VoiceChannel; } if (targetChannel == null) { await context.InlineReplyAsync($"{this._einherjiOptions.CurrentValue.FailureSymbol} You need to either provide a voice channel ID, or be in a voice channel currently. Also ensure I have access to that voice channel.", cancellationToken).ConfigureAwait(false); } return(new VoiceChannelMatch(targetChannel, user)); }
private async Task CmdGetAsync(SocketCommandContext context, Match match, CancellationToken cancellationToken = default) { // check if command has game name using IDisposable logScope = _log.BeginCommandScope(context, this); if (match.Groups.Count < 2 || match.Groups[1]?.Length < 1) { await SendNameRequiredAsync(context.Channel, cancellationToken).ConfigureAwait(false); return; } // get server info string gameName = match.Groups[1].Value.Trim(); GameServer server = await _gameServersStore.GetAsync(gameName, cancellationToken).ConfigureAwait(false); if (server == null) { _log.LogDebug("Server for game {Game} not found", gameName); await SendServerNotFoundAsync(context.Channel, gameName, cancellationToken).ConfigureAwait(false); return; } // check permissions if (!await IsAuthorizedAsync(context, server).ConfigureAwait(false)) { _log.LogTrace("User {UserID} not authorized for server for game {Game} not found", context.User.Id, gameName); await SendUnatuthorizedAsync(context.Channel, server, cancellationToken).ConfigureAwait(false); return; } // build server info embed EmbedBuilder embed = new EmbedBuilder(); embed.Title = $"{server.Game} Server Info"; embed.Color = Color.Blue; StringBuilder builder = new StringBuilder(); if (!string.IsNullOrWhiteSpace(server.RulesURL)) { builder.AppendFormat("Before connecting, please read the [server rules]({0}).\n\n", server.RulesURL); } builder.AppendFormat("***Address***: `{0}`\n", server.Address); if (!string.IsNullOrWhiteSpace(server.Password)) { builder.AppendFormat("***Password***: `{0}`\n", server.Password); } embed.Description = builder.ToString(); // send response - directly if PM, or direct to user's PM if in guild string text = this.IsAutoRemoving ? GetAutoremoveText() : null; IUserMessage sentMsg; if (context.IsPrivate) { sentMsg = await context.ReplyAsync(text, false, embed.Build(), cancellationToken).ConfigureAwait(false); } else { _ = context.InlineReplyAsync($"{_einherjiOptions.SuccessSymbol} I will send you a private message with info on how to connect to the server!"); Task <IUserMessage> pmTask = context.User.SendMessageAsync(text, false, embed.Build(), new RequestOptions { CancelToken = cancellationToken }); sentMsg = await pmTask.ConfigureAwait(false); } // auto remove if (this.IsAutoRemoving) { RemoveMessagesDelayed(_gameServersOptions.AutoRemoveDelay, cancellationToken, sentMsg); } }