private async Task <IWebhook> GetWebhook(ITextChannel channel, IGuildUser user) { EmojiWebhook found = (await _container.Query($"SELECT * FROM db WHERE db.UserId = {user.Id}")).FirstOrDefault(); if (found == null) { var webhook = await channel.CreateWebhookAsync(user.Nickname ?? user.Username); EmojiWebhook entry = new EmojiWebhook() { UserId = user.Id, ChannelsAndWebhooks = new Dictionary <ulong, ulong>() { [channel.Id] = webhook.Id } }; await _container.Insert(entry); return(webhook); } else { if (found.ChannelsAndWebhooks.TryGetValue(channel.Id, out var value)) { return(await channel.GetWebhookAsync(value)); } else { var webhook = await channel.CreateWebhookAsync(user.Nickname ?? user.Username); found.ChannelsAndWebhooks.Add(channel.Id, webhook.Id); await _container.Upsert(found); return(webhook); } } }
public async Task TestAsync(string command, string name = "Default Name", string msg = "") { switch (command) { case "webhook": ITextChannel t = Context.Channel as ITextChannel; var webhook = await t.CreateWebhookAsync(name); DiscordWebhookClient w = new DiscordWebhookClient(webhook); await w.SendMessageAsync(msg); await w.DeleteWebhookAsync(); await webhook.DeleteAsync(); break; case "box": ulong id = 0; try { ITextChannel here = Context.Channel as ITextChannel; var iBox = await here.CreateWebhookAsync("Touhou Box"); id = iBox.Id; DiscordWebhookClient weeb = new DiscordWebhookClient(iBox); await weeb.ModifyWebhookAsync(x => { x.Image = new Image(GetStreamFromURL("http://img.zeryx.xyz/LewdBoxLogo.jpg")); }); await weeb.SendFileAsync(GetStreamFromURL("http://img.zeryx.xyz/lewdboxes/touhou/cir001.png"), "cir001.png", "You got Cirno!"); await weeb.DeleteWebhookAsync(); break; } catch (Exception e) { Log(e.Message); ITextChannel tempc = Context.Channel as ITextChannel; var tempw = await tempc.GetWebhookAsync(id); await tempw.DeleteAsync(); break; } } }
public async Task <DiscordWebhookClient> GetWebhook(ITextChannel channel, DiscordSocketClient discord) { if (!hookClients.TryGetValue(channel.Id, out var client)) { var hooks = await channel.GetWebhooksAsync(); var hook = hooks.FirstOrDefault(h => h.Creator.Id == discord.CurrentUser.Id) ?? await channel.CreateWebhookAsync($"DMRP Proxy Hook"); client = new DiscordWebhookClient(hook); hookClients[channel.Id] = client; } return(client); }
private async Task <DiscordWebhookClient> CreateWebhookAsync(string name, ITextChannel chan) { try { if (!CanUseWebhooks(chan)) { return(null); } IWebhook webhook = await chan.CreateWebhookAsync(name); return(new DiscordWebhookClient(webhook)); } catch (Exception ex) { this.Logger.Nice("Webhook", ConsoleColor.Red, $"Could not create webhook: {ex.Message}"); return(null); } }
public async Task <DiscordWebhookClient> GetWebhookClientAsync(ITextChannel channel) { _lock.WaitOne(); try { if (cache.TryGetValue(channel.Id, out var correct)) { return(new DiscordWebhookClient(correct)); } var hooks = await channel.GetWebhooksAsync(); correct = hooks.FirstOrDefault(x => x.Name == _name); if (correct == null) { correct = await channel.CreateWebhookAsync(_name); } cache[channel.Id] = correct; return(new DiscordWebhookClient(correct)); } finally { _lock.Release(); } }
private Task <IWebhook> DoCreateWebhook(ITextChannel channel) { _logger.Information("Creating new webhook for channel {Channel}", channel.Id); return(channel.CreateWebhookAsync(WebhookName)); }