Beispiel #1
0
        public async Task Puppet(SocketUser user = null, [Remainder] string say = "")
        {
            if (user == null)
            {
                await BetterReplyAsync("You must mention a user to make them talk.", parameters : $"user null; {say}");

                return;
            }

            if (string.IsNullOrWhiteSpace(say))
            {
                await BetterReplyAsync($"You didn't tell {BetterUserFormat(user)} what to say.", parameters : $"{user} ({user.Id}); {say}");

                return;
            }

            await Context.Channel.TriggerTypingAsync();

            if (Context.Channel is SocketTextChannel channel)
            {
                Task <MemoryStream> download = Http.GetStreamAsync(user.GetAvatarUrl(ImageFormat.Png, 256));

                using MemoryStream avatar = await download;
                if (download.IsCompletedSuccessfully)
                {
                    RestWebhook webhook = await channel.CreateWebhookAsync(BetterUserFormat(user, true), avatar);

                    try {
                        using (DiscordWebhookClient webhookClient = new DiscordWebhookClient(webhook)) {
                            await webhookClient.SendMessageAsync(say);
                        }

                        await LogUserMessageAsync("Head-Non Sub Puppet", $"User {Context.User} ({Context.User.Id}) made {user} ({user.Id}) say: {say}");
                    } catch (Exception ex) {
                        LoggingManager.Log.Error(ex);
                    } finally {
                        await webhook.DeleteAsync();
                    }
                }
            }
        }
Beispiel #2
0
 internal static string GetAvatarUrl(RestWebhook webhook, ImageFormat format = default, int size = 2048)
 => webhook.AvatarHash != null
         ? GetUserAvatarUrl(webhook.Id, webhook.AvatarHash, format, size)
         : GetDefaultUserAvatarUrl(DefaultAvatarColor.Blurple);
Beispiel #3
0
        private async void EmoteDetection()
        {
            if (SocketGuildChannel != null)
            {
                // this emote cant be send because its occupied by a command
                if (CommandInfos.Any(i => i.ToLower() == SocketMessage.Content.ToLower().Replace(Program.CurrentPrefix, "")))
                {
                    return;
                }

                if (SocketMessage.Content.StartsWith(Program.CurrentPrefix))
                {
                    // check if the emoji exists and if the emojis is animated
                    string name  = SocketMessage.Content.Substring(Program.CurrentPrefix.Length, SocketMessage.Content.Length - Program.CurrentPrefix.Length);
                    int    index = -1;

                    if (name.Contains('-'))
                    {
                        int.TryParse(name.Substring(name.IndexOf('-') + 1, name.Length - name.IndexOf('-') - 1), out index);
                        name = name.Substring(0, name.IndexOf('-')); // take only the emote name
                    }

                    var          favEmote = DatabaseManager.EmoteDatabaseManager.GetFavouriteEmote(SocketGuildUser.Id, name);
                    DiscordEmote emote    = null;


                    if (favEmote != null)
                    {
                        // Load the emote info
                        emote = DatabaseManager.EmoteDatabaseManager.GetDiscordEmoteById(favEmote.DiscordEmoteId);
                    }
                    else
                    {
                        var emotes = DatabaseManager.EmoteDatabaseManager.GetEmotesByDirectName(name);

                        if (index < 1)
                        {
                            emote = emotes?.FirstOrDefault();
                        }
                        else
                        {
                            emote = emotes?.Skip(index - 1)?.FirstOrDefault();

                            // prevent null ref error
                            if (emote == null)
                            {
                                return;
                            }

                            emote.EmoteName += $"-{index}";
                        }
                    }

                    if (emote != null)
                    {
                        RestWebhook          webhook       = null;
                        DiscordWebhookClient webhookClient = null;
                        try
                        {
                            var channelWebhooks = await SocketTextChannel.GetWebhooksAsync();

                            webhook = channelWebhooks.SingleOrDefault(i => i.Name == "BattleRush's Helper"); // TODO Do over ApplicationId

                            if (webhook == null)
                            {
                                FileStream file = new FileStream(Path.Combine(Program.ApplicationSetting.BasePath, "Images", "BRH_Logo.png"), FileMode.Open);

                                // Config name
                                await SocketTextChannel.CreateWebhookAsync("BattleRush's Helper", file);

                                channelWebhooks = await SocketTextChannel.GetWebhooksAsync();
                            }

                            webhook = channelWebhooks.SingleOrDefault(i => i.Name == "BattleRush's Helper"); // TODO Do over ApplicationId

                            if (SocketThreadChannel == null)
                            {
                                webhookClient = new DiscordWebhookClient(webhook.Id, webhook.Token);
                            }
                            else
                            {
                                webhookClient = new DiscordWebhookClient($"https://discord.com/api/webhooks/{webhook.Id}/{webhook.Token}?thread_id={SocketThreadChannel.Id}");
                            }
                        }
                        catch (Exception ex)
                        {
                            // likeky no webhook perms -> skip
                        }

                        await SocketMessage.DeleteAsync();

                        // TODO Keep relevant webhook infos in cache


                        string avatarUrl = SocketGuildUser.GetGuildAvatarUrl();
                        if (avatarUrl == null)
                        {
                            avatarUrl = SocketGuildUser.GetAvatarUrl();
                        }

                        if (SocketGuild.Emotes.Any(i => i.Id == emote.DiscordEmoteId))
                        {
                            var emoteString = $"<{(emote.Animated ? "a" : "")}:{SocketGuild.Emotes.First(i => i.Id == emote.DiscordEmoteId).Name}:{emote.DiscordEmoteId}>";

                            // we can post the emote as it will be rendered out
                            //await SocketTextChannel.SendMessageAsync(emoteString);
                            if (webhookClient != null)
                            {
                                await webhookClient.SendMessageAsync(emoteString, false, null, SocketGuildUser.Nickname ?? SocketGuildUser.Username, avatarUrl);
                            }
                            else
                            {
                                await SocketMessage.Channel.SendMessageAsync(emoteString, false, null, null, null, new MessageReference(SocketMessage.ReferencedMessage?.Id));
                            }
                        }
                        else
                        {
                            FileAttachment fileAttachment = new FileAttachment(emote.LocalPath, null, name, false);

                            // TODO store resized images in db for faster reuse
                            // TODO use images from filesystem -> no web call
                            if (emote.Animated)
                            {
                                // TODO gif resize
                                //await SocketTextChannel.SendMessageAsync(emote.Url);

                                //
                                if (webhookClient != null)
                                {
                                    //await webhookClient.SendFileAsync(emote.LocalPath, "", false, null, SocketGuildUser.Nickname ?? SocketGuildUser.Username, avatarUrl);
                                    await webhookClient.SendFileAsync(fileAttachment, "", false, null, SocketGuildUser.Nickname ?? SocketGuildUser.Username, avatarUrl);
                                }
                                else
                                {
                                    await SocketMessage.Channel.SendFileAsync(emote.LocalPath, "", false, null, null, false, null, new MessageReference(SocketMessage.ReferencedMessage?.Id));
                                }
                            }
                            else
                            {
                                SKBitmap bmp;
                                using (var ms = new MemoryStream(File.ReadAllBytes(emote.LocalPath)))
                                    bmp = SKBitmap.Decode(ms);

                                var resImage = CommonHelper.ResizeImage(bmp, Math.Min(bmp.Height, 48));
                                var stream   = CommonHelper.GetStream(resImage);

                                fileAttachment = new FileAttachment(stream, $"{emote.EmoteName}.png", name, false);

                                if (webhookClient != null)
                                {
                                    //await webhookClient.SendFileAsync(stream, $"{emote.EmoteName}.png", "", false, null, SocketGuildUser.Nickname ?? SocketGuildUser.Username, avatarUrl);
                                    await webhookClient.SendFileAsync(fileAttachment, "", false, null, SocketGuildUser.Nickname ?? SocketGuildUser.Username, avatarUrl);
                                }
                                else
                                {
                                    await SocketMessage.Channel.SendFileAsync(stream, $"{emote.EmoteName}.png", "", false, null, null, false, null, new MessageReference(SocketMessage.ReferencedMessage?.Id));
                                }
                            }
                        }

                        // In case the webhook could not be created
                        if (webhookClient == null)
                        {
                            await SocketMessage.Channel.SendMessageAsync($"({Program.CurrentPrefix}{emote.EmoteName}) by <@{SocketGuildUser.Id}>");
                        }
                    }
                }
            }
        }