mirror of
https://github.com/RPCS3/discord-bot.git
synced 2024-12-12 05:06:14 +00:00
show invite links in dm
This commit is contained in:
parent
0956410d69
commit
2e996fa9a9
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CompatApiClient.Utils;
|
||||
@ -22,11 +23,18 @@ namespace CompatBot.Commands
|
||||
[Description("Lists all filters")]
|
||||
public async Task List(CommandContext ctx)
|
||||
{
|
||||
var result = new StringBuilder("```")
|
||||
.AppendLine($"ID | {"Guild ID",-18} | Guild Name")
|
||||
.AppendLine("-------------------------------------------------");
|
||||
const string linkPrefix = "discord.gg/";
|
||||
using (var db = new BotDb())
|
||||
{
|
||||
var maxInviteLength = await db.WhitelistedInvites.Select(i => i.InviteCode).Where(c => c != null).MaxAsync(c => c.Length).ConfigureAwait(false);
|
||||
var linkLength = linkPrefix.Length + maxInviteLength;
|
||||
var header = $"ID | {"Server ID",-18}";
|
||||
if (ctx.Channel.IsPrivate)
|
||||
header += $" | {"Invite link".PadRight(linkLength)}";
|
||||
header += " | Server Name";
|
||||
var result = new StringBuilder("```")
|
||||
.AppendLine(header)
|
||||
.AppendLine("".PadRight(header.Length + 10, '-'));
|
||||
var whitelistedInvites = await db.WhitelistedInvites.ToListAsync().ConfigureAwait(false);
|
||||
if (whitelistedInvites.Count == 0)
|
||||
{
|
||||
@ -52,11 +60,18 @@ namespace CompatBot.Commands
|
||||
}
|
||||
catch { }
|
||||
if (string.IsNullOrEmpty(guildName))
|
||||
guildName = item.Name ?? "Failed to resolve";
|
||||
result.AppendLine($"{item.Id:0000} | {item.GuildId,-18} | {guildName.Sanitize()}");
|
||||
guildName = item.Name ?? "";
|
||||
var link = "";
|
||||
if (!string.IsNullOrEmpty(item.InviteCode))
|
||||
link = linkPrefix + item.InviteCode;
|
||||
//discord expands invite links even if they're inside the code block for some reason
|
||||
result.Append($"{item.Id:0000} | {item.GuildId,-18}");
|
||||
if (ctx.Channel.IsPrivate)
|
||||
result.Append(" | \u200d").Append(link.PadRight(linkLength));
|
||||
result.Append(" | ").AppendLine(guildName.Sanitize());
|
||||
}
|
||||
}
|
||||
await ctx.SendAutosplitMessageAsync(result.Append("```")).ConfigureAwait(false);
|
||||
await ctx.SendAutosplitMessageAsync(result.Append("```")).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("whitelist"), Aliases("add", "allow"), Priority(10)]
|
||||
|
@ -119,6 +119,7 @@ namespace CompatBot.EventHandlers
|
||||
if (inviteCodes.Count == 0 && discordMeLinks.Count == 0 && !tryMessageAsACode)
|
||||
return (false, new List<DiscordInvite>(0));
|
||||
|
||||
var hasInvalidInvites = false;
|
||||
foreach (var meLink in discordMeLinks)
|
||||
{
|
||||
try
|
||||
@ -129,9 +130,10 @@ namespace CompatBot.EventHandlers
|
||||
request.Headers.CacheControl = CacheControlHeaderValue.Parse("no-cache");
|
||||
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("RPCS3CompatibilityBot", "2.0"));
|
||||
using (var response = await HttpClient.SendAsync(request))
|
||||
{
|
||||
var html = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var html = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
if (string.IsNullOrEmpty(html))
|
||||
continue;
|
||||
|
||||
@ -139,7 +141,11 @@ namespace CompatBot.EventHandlers
|
||||
inviteCodes.Add(match.Groups["invite_id"].Value);
|
||||
}
|
||||
else
|
||||
client.DebugLogger.LogMessage(LogLevel.Warning, "", $"Got {response.StatusCode} from discord.me", DateTime.Now);
|
||||
{
|
||||
hasInvalidInvites = true;
|
||||
client.DebugLogger.LogMessage(LogLevel.Warning, "", $"Got {response.StatusCode} from discord.me: {html}", DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
@ -153,7 +159,6 @@ namespace CompatBot.EventHandlers
|
||||
inviteCodes = inviteCodes.Distinct().Where(s => !string.IsNullOrEmpty(s)).ToList();
|
||||
|
||||
var result = new List<DiscordInvite>(inviteCodes.Count);
|
||||
var hasInvalidInvites = false;
|
||||
foreach (var inviteCode in inviteCodes)
|
||||
try
|
||||
{
|
||||
@ -163,7 +168,7 @@ namespace CompatBot.EventHandlers
|
||||
catch (Exception e)
|
||||
{
|
||||
hasInvalidInvites = true;
|
||||
client.DebugLogger.LogMessage(LogLevel.Warning, "", $"Failed to get invite for code {inviteCode}: {e}", DateTime.Now);
|
||||
client.DebugLogger.LogMessage(LogLevel.Warning, "", $"Failed to get invite for code {inviteCode}: {e.Message}", DateTime.Now);
|
||||
}
|
||||
return (hasInvalidInvites, result);
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.IO.Pipelines;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
@ -21,7 +20,6 @@ namespace CompatBot.ThumbScrapper
|
||||
private static readonly HttpClient HttpClient = HttpClientFactory.Create(new CompressionMessageHandler());
|
||||
private static readonly Uri TitleDownloadLink = new Uri("https://www.gametdb.com/ps3tdb.zip?LANG=EN");
|
||||
|
||||
|
||||
public static async Task RunAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
do
|
||||
@ -41,7 +39,7 @@ namespace CompatBot.ThumbScrapper
|
||||
} while (!cancellationToken.IsCancellationRequested);
|
||||
}
|
||||
|
||||
public static async Task UpdateGameTitlesAsync(CancellationToken cancellationToken)
|
||||
private static async Task UpdateGameTitlesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var container = Path.GetFileName(TitleDownloadLink.AbsolutePath);
|
||||
try
|
||||
|
Loading…
Reference in New Issue
Block a user