Skip to content

Commit

Permalink
Merge branch 'main' into floatingmilkshake/dsp-command-migration
Browse files Browse the repository at this point in the history
# Conflicts:
#	Commands/DebugCmds.cs
  • Loading branch information
FloatingMilkshake committed Dec 10, 2024
2 parents 1dabc79 + 1996b17 commit 810d9bc
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 41 deletions.
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ CLIPTOK_ANTIPHISHING_ENDPOINT=useyourimagination
CLOUDFLARED_TOKEN=ignoreifnotrelevant
USERNAME_CHECK_ENDPOINT=https://api.example.com/username
CLIPTALK_WEBHOOK=https://discord.com
REACTION_LOG_WEBHOOK=https://discord.com
UPTIME_KUMA_PUSH_URL=
TS_AUTHKEY=tskey-auth-asdfg-asdfghj
120 changes: 106 additions & 14 deletions Commands/DebugCmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,11 @@ await ctx.RespondAsync(
var response = $"**Overrides for {user.Mention}:**\n\n";
foreach (var overwrite in overwrites)
{
var allowedPermissions = string.IsNullOrWhiteSpace(overwrite.Value.Allowed.ToString("name")) ? "none" : overwrite.Value.Allowed.ToString("name");
var deniedPermissions = string.IsNullOrWhiteSpace(overwrite.Value.Denied.ToString("name")) ? "none" : overwrite.Value.Denied.ToString("name");

response +=
$"<#{overwrite.Key}>:\n**Allowed**: {overwrite.Value.Allowed}\n**Denied**: {overwrite.Value.Denied}\n\n";
$"<#{overwrite.Key}>:\n**Allowed**: {allowedPermissions}\n**Denied**: {deniedPermissions}\n\n";
}

if (response.Length > 2000)
Expand Down Expand Up @@ -473,23 +476,112 @@ public async Task Apply(TextCommandContext ctx,

await msg.ModifyAsync(x => x.Content = $"{Program.cfgjson.Emoji.Success} Successfully applied {numAppliedOverrides}/{dictionary.Count} overrides for {user.Mention}!");
}
}

[Command("dump")]
[Description("Dump all of a channel's overrides from Discord or the database.")]
[IsBotOwner]
public class DumpChannelOverrides
{
[DefaultGroupCommand]
[Command("discord")]
[Description("Dump all of a channel's overrides as they exist on the Discord channel. Does not read from db.")]
public async Task DumpFromDiscord(CommandContext ctx,
[Description("The channel to dump overrides for.")] DiscordChannel channel)
{
var overwrites = channel.PermissionOverwrites;

[Command("dumpchanneloverrides")]
[Description("Dump all of a channel's overrides. This pulls from Discord, not the database.")]
[IsBotOwner]
public async Task DumpChannelOverrides(TextCommandContext ctx,
[Description("The channel to dump overrides for.")] DiscordChannel channel)
{
var overwrites = channel.PermissionOverwrites;
string output = "";
foreach (var overwrite in overwrites)
{
output += $"{JsonConvert.SerializeObject(overwrite)}\n";
}

string output = "";
foreach (var overwrite in overwrites)
{
output += $"{JsonConvert.SerializeObject(overwrite)}\n";
await ctx.RespondAsync($"Dump from Discord:\n{await StringHelpers.CodeOrHasteBinAsync(output, "json")}");
}

[Command("db")]
[TextAlias("database")]
[Description("Dump all of a channel's overrides as they are stored in the db.")]
public async Task DumpFromDb(CommandContext ctx,
[Description("The channel to dump overrides for.")] DiscordChannel channel)
{
List<DiscordOverwrite> overwrites = new();
try
{
var allOverwrites = await Program.db.HashGetAllAsync("overrides");
foreach (var overwrite in allOverwrites) {
var overwriteDict = JsonConvert.DeserializeObject<Dictionary<ulong, DiscordOverwrite>>(overwrite.Value);
if (overwriteDict is null) continue;
if (overwriteDict.TryGetValue(channel.Id, out var value))
overwrites.Add(value);
}
}
catch (Exception ex)
{
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} Something went wrong while trying to fetch the overrides for {channel.Mention}!" +
" There are overrides in the database but I could not parse them. Check the database manually for details.");

Program.discord.Logger.LogError(ex, "Failed to read overrides from db for 'debug overrides dump'!");

return;
}

if (overwrites.Count == 0)
{
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Error} No overrides found for {channel.Mention} in the database!");
return;
}

string output = "";
foreach (var overwrite in overwrites)
{
output += $"{JsonConvert.SerializeObject(overwrite)}\n";
}

await ctx.RespondAsync($"Dump from db:\n{await StringHelpers.CodeOrHasteBinAsync(output, "json")}");
}
}

[Command("cleanup")]
[Aliases("clean", "prune")]
[Description("Removes overrides from the db for channels that no longer exist.")]
[IsBotOwner]
public async Task CleanUpOverrides(CommandContext ctx)
{
var msg = await ctx.RespondAsync($"{Program.cfgjson.Emoji.Loading} Working on it...");
var removedOverridesCount = 0;

var dbOverwrites = await Program.db.HashGetAllAsync("overrides");
foreach (var userOverwrites in dbOverwrites)
{
var overwriteDict = JsonConvert.DeserializeObject<Dictionary<ulong, DiscordOverwrite>>(userOverwrites.Value);
foreach (var overwrite in overwriteDict)
{
bool channelExists = Program.discord.Guilds.Any(g => g.Value.Channels.Any(c => c.Key == overwrite.Key));

await ctx.RespondAsync(await StringHelpers.CodeOrHasteBinAsync(output, "json"));
if (!channelExists)
{
// Channel no longer exists, remove the override
overwriteDict.Remove(overwrite.Key);
removedOverridesCount++;
}
}

// Write back to db
// If the user now has no overrides, remove them from the db entirely
if (overwriteDict.Count == 0)
{
await Program.db.HashDeleteAsync("overrides", userOverwrites.Name);
}
else
{
// Otherwise, update the user's overrides in the db
await Program.db.HashSetAsync("overrides", userOverwrites.Name, JsonConvert.SerializeObject(overwriteDict));
}
}

await msg.ModifyAsync($"{Program.cfgjson.Emoji.Success} Done! Cleaned up {removedOverridesCount} overrides.");
}
}

[Command("dmchannel")]
Expand Down
6 changes: 6 additions & 0 deletions Constants/RegexConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ public class RegexConstants
readonly public static Regex bold_rx = new("\\*\\*(.*?)\\*\\*");
readonly public static Regex discord_link_rx = new(@".*discord(?:app)?.com\/channels\/((?:@)?[a-z0-9]*)\/([0-9]*)(?:\/)?([0-9]*)");
readonly public static Regex channel_rx = new("<#([0-9]+)>");
readonly public static Regex warn_msg_rx = new($"{Program.cfgjson.Emoji.Warning} <@!?[0-9]+> was warned");
readonly public static Regex auto_warn_msg_rx = new($"{Program.cfgjson.Emoji.Denied} <@!?[0-9]+> was automatically warned");
readonly public static Regex mute_msg_rx = new($"{Program.cfgjson.Emoji.Muted} <@!?[0-9]+> has been muted");
readonly public static Regex unmute_msg_rx = new($"{Program.cfgjson.Emoji.Information} Successfully unmuted");
readonly public static Regex ban_msg_rx = new($"{Program.cfgjson.Emoji.Banned} <@!?[0-9]+> has been banned");
readonly public static Regex unban_msg_rx = new($"{Program.cfgjson.Emoji.Unbanned} Successfully unbanned");
}
}
10 changes: 9 additions & 1 deletion Events/ChannelEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
{
public class ChannelEvents
{
public static async Task ChannelCreated(DiscordClient _, ChannelCreatedEventArgs e)
{
// see comment on ChannelUpdated

var timestamp = DateTime.Now;
Tasks.EventTasks.PendingChannelCreateEvents.Add(timestamp, e);
}

public static async Task ChannelUpdated(DiscordClient _, ChannelUpdatedEventArgs e)
{
// Add this event to the pending events list. These are handled in a task later, see Tasks/EventTasks/HandlePendingChannelUpdateEventsAsync
Expand All @@ -12,7 +20,7 @@ public static async Task ChannelUpdated(DiscordClient _, ChannelUpdatedEventArgs

public static async Task ChannelDeleted(DiscordClient client, ChannelDeletedEventArgs e)
{
// see above
// see comment on ChannelUpdated

var timestamp = DateTime.Now;
Tasks.EventTasks.PendingChannelDeleteEvents.Add(timestamp, e);
Expand Down
52 changes: 31 additions & 21 deletions Events/MemberEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,43 @@ public static async Task GuildMemberRemoved(DiscordClient client, GuildMemberRem
if (e.Guild.Id != cfgjson.ServerID)
return;

var muteRole = await e.Guild.GetRoleAsync(cfgjson.MutedRole);
// Attempt to check if member is cached
bool isMemberCached = client.Guilds[e.Guild.Id].Members.ContainsKey(e.Member.Id);

DiscordRole tqsMuteRole = default;
if (cfgjson.TqsMutedRole != 0)
tqsMuteRole = await e.Guild.GetRoleAsync(cfgjson.TqsMutedRole);
if (isMemberCached)
{
// Only check mute role against db entry if we know the member's roles are accurate.
// If the member is not cached, we will think they have no roles when they might actually be muted!
// Then we would be falsely removing their mute entry.

var userMute = await db.HashGetAsync("mutes", e.Member.Id);
var muteRole = await e.Guild.GetRoleAsync(cfgjson.MutedRole);

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) & !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
DiscordRole tqsMuteRole = default;
if (cfgjson.TqsMutedRole != 0)
tqsMuteRole = await e.Guild.GetRoleAsync(cfgjson.TqsMutedRole);

if ((e.Member.Roles.Contains(muteRole) || e.Member.Roles.Contains(tqsMuteRole)) && userMute.IsNull)
{
MemberPunishment newMute = new()
var userMute = await db.HashGetAsync("mutes", e.Member.Id);

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) & !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);

if ((e.Member.Roles.Contains(muteRole) || e.Member.Roles.Contains(tqsMuteRole)) && userMute.IsNull)
{
MemberId = e.Member.Id,
ModId = discord.CurrentUser.Id,
ServerId = e.Guild.Id,
ExpireTime = null,
ActionTime = DateTime.Now
};

db.HashSetAsync("mutes", e.Member.Id, JsonConvert.SerializeObject(newMute));
}
MemberPunishment newMute = new()
{
MemberId = e.Member.Id,
ModId = discord.CurrentUser.Id,
ServerId = e.Guild.Id,
ExpireTime = null,
ActionTime = DateTime.Now
};

db.HashSetAsync("mutes", e.Member.Id, JsonConvert.SerializeObject(newMute));
}

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) && !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) && !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
}

string rolesStr = "None";

Expand Down
2 changes: 1 addition & 1 deletion Events/MessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe
// still warn anyway
}

DiscordMessage msg = await WarningHelpers.SendPublicWarningMessageAndDeleteInfringingMessageAsync(message, $"{{Program.cfgjson.Emoji.Denied}} {{message.Author.Mention}} was automatically warned: **{{reason.Replace(\"`\", \"\\\\`\").Replace(\"*\", \"\\\\*\")}}**", wasAutoModBlock, 1);
DiscordMessage msg = await WarningHelpers.SendPublicWarningMessageAndDeleteInfringingMessageAsync(message, $"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**", wasAutoModBlock, 1);
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, channel, " automatically ");
await InvestigationsHelpers.SendInfringingMessaageAsync("investigations", message, reason, warning.ContextLink, wasAutoModBlock: wasAutoModBlock);
match = true;
Expand Down
26 changes: 25 additions & 1 deletion Events/ReactionEvent.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
using static Cliptok.Program;
using static Cliptok.Constants.RegexConstants;

namespace Cliptok.Events
{
public class ReactionEvent
{
public static async Task OnReaction(DiscordClient _, MessageReactionAddedEventArgs e)
{
if (e.Emoji.Id != cfgjson.HeartosoftId || e.Channel.IsPrivate || e.Guild.Id != cfgjson.ServerID)
// Ignore DMs and other servers
if (e.Channel.IsPrivate || e.Guild.Id != cfgjson.ServerID)
return;

DiscordMessage targetMessage = await e.Channel.GetMessageAsync(e.Message.Id);

// Remove reactions from warning/mute/ban messages

if (targetMessage.Author.Id == discord.CurrentUser.Id &&
warn_msg_rx.IsMatch(targetMessage.Content) ||
auto_warn_msg_rx.IsMatch(targetMessage.Content) ||
mute_msg_rx.IsMatch(targetMessage.Content) ||
unmute_msg_rx.IsMatch(targetMessage.Content) ||
ban_msg_rx.IsMatch(targetMessage.Content) ||
unban_msg_rx.IsMatch(targetMessage.Content))
{
await targetMessage.DeleteReactionAsync(e.Emoji, e.User);
var emoji = e.Emoji.Id != 0 ? $"[{e.Emoji.Name}](<{e.Emoji.Url}>)" : e.Emoji.ToString();
await LogChannelHelper.LogMessageAsync("reactions", $"<:WindowsRecycleBin:824380487920910348> Removed reaction {emoji} from {e.Message.JumpLink} by {e.User.Mention}");
return;
}

// Remove self-heartosofts

if (e.Emoji.Id != cfgjson.HeartosoftId)
return;

// Avoid starboard race conditions
await Task.Delay(1000);

if (targetMessage.Author.Id == e.User.Id)
Expand Down
6 changes: 6 additions & 0 deletions Lists/scams.txt
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,9 @@ repackme8
JPYVouTegYI
nolerawin.com
?promo=TOWER10
nicholaswallace23
you will reimburse me 10% of your profits when you receive
interested on how to start earning $100k
Free deposit 100$
Promocode:Open2024
gambler.icu
2 changes: 2 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ static async Task Main(string[] _)
.HandleThreadMembersUpdated(ThreadEvents.Discord_ThreadMembersUpdated)
.HandleGuildBanRemoved(UnbanEvent.OnUnban)
.HandleVoiceStateUpdated(VoiceEvents.VoiceStateUpdate)
.HandleChannelCreated(ChannelEvents.ChannelCreated)
.HandleChannelUpdated(ChannelEvents.ChannelUpdated)
.HandleChannelDeleted(ChannelEvents.ChannelDeleted)
.HandleAutoModerationRuleExecuted(AutoModEvents.AutoModerationRuleExecuted)
Expand Down Expand Up @@ -254,6 +255,7 @@ static async Task Main(string[] _)
Tasks.ReminderTasks.CheckRemindersAsync(),
Tasks.RaidmodeTasks.CheckRaidmodeAsync(cfgjson.ServerID),
Tasks.LockdownTasks.CheckUnlocksAsync(),
Tasks.EventTasks.HandlePendingChannelCreateEventsAsync(),
Tasks.EventTasks.HandlePendingChannelUpdateEventsAsync(),
Tasks.EventTasks.HandlePendingChannelDeleteEventsAsync(),
];
Expand Down
Loading

0 comments on commit 810d9bc

Please sign in to comment.