public async Task RemoveRelayCommandAsync( [Description("Discord Channel to remove relay for.")] DiscordChannel discordChannel) { var cfg = await _database.FindAsync <RelayConfiguration>(Context.Channel.GuildId); if (cfg is null) { await RespondBasicError("No relays found."); return; } var discordHooks = await discordChannel.GetWebhooksAsync(); foreach (var hook in discordHooks) { if (cfg.DiscordToIRCLinks.TryGetValue(hook.Id, out var data)) { if (cfg.Webhooks.TryGetValue(data, out var hookData)) { await RemoveRelayCommandAsync(hookData.Id); return; } } } await RespondBasicError("No relays found."); }
private async Task <DiscordWebhook> FindExistingWebhook(DiscordChannel channel) { _logger.Debug("Finding webhook for channel {Channel}", channel.Id); try { return((await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine)); } catch (HttpRequestException e) { _logger.Warning(e, "Error occurred while fetching webhook list"); // This happens sometimes when Discord returns a malformed request for the webhook list // Nothing we can do than just assume that none exist and return null. return(null); } }
private async Task <IReadOnlyList <DiscordWebhook> > FetchChannelWebhooks(DiscordChannel channel) { try { return(await channel.GetWebhooksAsync()); } catch (HttpRequestException e) { _logger.Warning(e, "Error occurred while fetching webhook list"); // This happens sometimes when Discord returns a malformed request for the webhook list // Nothing we can do than just assume that none exist. return(new DiscordWebhook[0]); } }
public async Task DeleteAllAsync(CommandContext ctx, [Description("Channel to list webhooks for.")] DiscordChannel channel = null) { channel = channel ?? ctx.Channel; IReadOnlyList <DiscordWebhook> whs = await channel.GetWebhooksAsync(); if (!await ctx.WaitForBoolReplyAsync($"Are you sure you want to delete {Formatter.Bold("ALL")} of the webhooks ({Formatter.Bold(whs.Count.ToString())} total)?")) { return; } await Task.WhenAll(whs.Select(w => w.DeleteAsync())); await this.InformAsync(ctx, $"Successfully deleted all webhooks!", important : false); }
public async Task DeleteAsync(CommandContext ctx, [Description("Name.")] string name, [Description("Channel to list webhooks for.")] DiscordChannel channel = null) { channel = channel ?? ctx.Channel; IEnumerable <DiscordWebhook> whs = await channel.GetWebhooksAsync(); DiscordWebhook wh = whs.SingleOrDefault(w => w.Name.ToLowerInvariant() == name.ToLowerInvariant()); if (wh is null) { throw new CommandFailedException($"Webhook with name {Formatter.InlineCode(name)} does not exist!"); } await wh.DeleteAsync(); await this.InformAsync(ctx, $"Successfully deleted webhook {Formatter.InlineCode(wh.Name)}!", important : false); }
public async Task <CommandResult> RunAsync(DiscordChannel channel) { if (channel.Guild.Id == Context.Guild.Id) { GuildData data = this.GetData <GuildData>(channel.Guild.Id.ToString()); if (data.Webhook != null) { return("Pins have already been configured for this server. Run this command without arguments to disable pin redirection."); } var webhooks = await channel.GetWebhooksAsync(); using (MemoryStream str = new MemoryStream()) using (Stream sourceStr = await _client.GetStreamAsync(Context.Client.CurrentUser.AvatarUrl)) { sourceStr.CopyTo(str); str.Seek(0, SeekOrigin.Begin); DiscordWebhook webhook = await channel.CreateWebhookAsync("WamBot Pins Webhook", str, $"Pins setup by {Context.Author.Username}#{Context.Author.Discriminator}"); Meta.WebhookClient.AddWebhook(webhook); await webhook.ExecuteAsync("Webhook setup!"); data.Webhook = webhook; DiscordMessage msg = await Context.ReplyAsync("Updating pins, this may take a while..."); await Meta.UpdatePinsAsync(DateTimeOffset.Now, Context.Channel, data, Context.Client); await msg.DeleteAsync(); this.SetData(channel.Guild.Id.ToString(), data); return("Pins channel configured and updated!"); } } else { return("Oi! You cannae do that!"); } }
public async Task ListAsync(CommandContext ctx, [Description("Channel to list webhooks for.")] DiscordChannel channel = null) { channel = channel ?? ctx.Channel; IReadOnlyList <DiscordWebhook> whs = await channel.GetWebhooksAsync(); if (!whs.Any()) { throw new CommandFailedException("There are no webhooks in this channel."); } bool displayToken = await ctx.WaitForBoolReplyAsync("Do you wish to display the tokens?", reply : false); await ctx.Client.GetInteractivity().SendPaginatedMessageAsync(ctx.Channel, ctx.User, whs.Select(wh => new Page(embed: new DiscordEmbedBuilder { Title = $"Webhook: {wh.Name}", Description = $"{(displayToken ? $"Token: {Formatter.InlineCode(wh.Token)}\n" : "")}Created by {wh.User.ToString()}", Color = this.ModuleColor, ThumbnailUrl = wh.AvatarUrl, Timestamp = wh.CreationTimestamp, }.AddField("URL", displayToken ? wh.BuildUrlString() : "Hidden"))) ); }
public async Task RegisterRelayCommandAsync( [Description("Discord channel to link to.")] DiscordChannel discordChannel, [Description("IRC channel to link to.")] string ircChannel) { ircChannel = GetIrcChannelName(ircChannel); if (Context.Channel.GuildId == null) { await RespondBasicError("Can only use relay command in a guild channel"); return; } var cfg = _database.Find <RelayConfiguration>(Context.Channel.GuildId); if (cfg is null) { cfg = new RelayConfiguration() { DiscordServer = Context.Channel.GuildId.Value }; await _database.AddAsync(cfg); await _database.SaveChangesAsync(); } // TODO: Some check to see if the IRC channel is available. if (cfg.DiscordToIRCLinks.ContainsKey(discordChannel.Id)) { await RespondBasicError("A relay already exists for this Discord channel."); } var activeHooks = await discordChannel.GetWebhooksAsync(); if (cfg.Webhooks.TryGetValue(ircChannel, out var hook)) { if (activeHooks.Any(x => x.Id == hook.Id)) { await RespondBasicError("A relay for this IRC channel is already in that Discord channel!"); } else { _database.Update(cfg); var discordHook = await Context.Client.GetWebhookAsync(hook.Id); await discordHook.ModifyAsync(channelId : discordChannel.Id); await _database.SaveChangesAsync(); await RespondBasicSuccess($"Moved the relay for IRC channel `{ircChannel}` to {discordChannel.Name}"); } return; } var newHook = await discordChannel.CreateWebhookAsync($"Dostya-{ircChannel}", reason : "Dostya Relay Creation"); if (await _relay.AddRelayAsync(Context.Channel.GuildId.Value, newHook, ircChannel)) { await RespondBasicSuccess($"Relay bridge added ({discordChannel.Name} <-> {ircChannel})"); } else { await RespondBasicError("Failed to add new relay."); } }