예제 #1
0
        public async Task Say(ICommand command)
        {
            var message = command["Message"].AsString ?? "";
            var channel = command["TargetChannel"].AsTextChannel;

            // This is a mods-only command, but to prevent permission escalation, check
            // if there's any non-mentionable role and if the sender has a mention everyone perm
            var nonMentionableRoles = command.Message.MentionedRoleIds.Where(x => !command.Guild.GetRole(x)?.IsMentionable ?? false).ToList();
            var replaceRoleMentions = (message.ContainsEveryonePings() || nonMentionableRoles.Any()) &&
                                      !((IGuildUser)command.Author).GetPermissions(channel).MentionEveryone;

            if (replaceRoleMentions)
            {
                message = DiscordHelpers.ReplaceRoleMentions(message, nonMentionableRoles, command.Guild)
                          .Sanitise(allowRoleMentions: true);
            }

            if (command.Message.Attachments.Count <= 0)
            {
                if (string.IsNullOrEmpty(command["Message"]))
                {
                    throw new Framework.Exceptions.IncorrectParametersCommandException("Specify a message or an attachment.");
                }

                await channel.SendMessageAsync(message);
            }
            else
            {
                var attachment = command.Message.Attachments.First();
                var request    = WebRequest.CreateHttp(attachment.Url);
                using (var response = await request.GetResponseAsync())
                    using (var stream = response.GetResponseStream())
                        using (var memStream = new MemoryStream())
                        {
                            await stream.CopyToAsync(memStream);

                            memStream.Position = 0;

                            await channel.SendFileAsync(memStream, attachment.Filename, message);
                        }
            }

            if (command["TargetChannel"].AsTextChannel.Id != command.Message.Channel.Id)
            {
                await command.ReplySuccess("Message sent!" + (replaceRoleMentions ? " To mention roles, @here, or @everyone you must have the Mention Everyone permission." : ""));
            }
        }