Ejemplo n.º 1
0
        public async Task <WebhookWrapper> CreateWebhookAsync(SocketTextChannel channel, string name)
        {
            var get = await GetWebhookAsync(channel, new WebhookOptions
            {
                Name = name
            });

            var webhook = get ?? await channel.CreateWebhookAsync(name, AvatarStream());

            return(new WebhookWrapper
            {
                TextChannel = channel.Id,
                WebhookId = webhook.Id,
                WebhookToken = webhook.Token
            });
        }
Ejemplo n.º 2
0
        public async Task <WebhookWrapper> CreateWebhookAsync(SocketTextChannel Channel, string Name)
        {
            var Get = await GetWebhookAsync(Channel, new WebhookOptions
            {
                Name = Name
            });

            var Webhook = Get ?? await Channel.CreateWebhookAsync(Name, AvatarStream());

            return(new WebhookWrapper
            {
                TextChannel = Channel.Id,
                WebhookId = Webhook.Id,
                WebhookToken = Webhook.Token
            });
        }
Ejemplo n.º 3
0
        public async Task <WebhookWrapper> UpdateWebhookAsync(SocketTextChannel Channel, WebhookWrapper Old, WebhookOptions Options)
        {
            var Hook = !(SocketClient.GetChannel(Old.TextChannel) is SocketTextChannel GetChannel) ?
                       await GetWebhookAsync(Channel.Guild, new WebhookOptions { Webhook = Old }) :
                       await GetWebhookAsync(GetChannel, new WebhookOptions { Webhook = Old });

            if (Channel.Id == Old.TextChannel && Hook != null)
            {
                return(Old);
            }
            else if (Hook != null)
            {
                await Hook.DeleteAsync();
            }
            var New = await Channel.CreateWebhookAsync(Options.Name, AvatarStream());

            return(new WebhookWrapper
            {
                TextChannel = Channel.Id,
                WebhookId = New.Id,
                WebhookToken = New.Token
            });
        }
Ejemplo n.º 4
0
        public async Task <WebhookWrapper> UpdateWebhookAsync(SocketTextChannel channel, WebhookWrapper old,
                                                              WebhookOptions options)
        {
            var hook = !(_client.GetChannel(old.TextChannel) is SocketTextChannel getChannel)
                ? await GetWebhookAsync(channel.Guild, new WebhookOptions { Webhook = old })
                : await GetWebhookAsync(getChannel, new WebhookOptions { Webhook = old });

            if (channel.Id == old.TextChannel && hook != null)
            {
                return(old);
            }
            if (hook != null)
            {
                await hook.DeleteAsync();
            }
            var New = await channel.CreateWebhookAsync(options.Name, AvatarStream());

            return(new WebhookWrapper
            {
                TextChannel = channel.Id,
                WebhookId = New.Id,
                WebhookToken = New.Token
            });
        }
Ejemplo n.º 5
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}>");
                        }
                    }
                }
            }
        }