Skip to content

Commit

Permalink
fixed triggers message not showing
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Dec 2, 2024
1 parent 5404ab0 commit 59c2c7b
Showing 1 changed file with 82 additions and 45 deletions.
127 changes: 82 additions & 45 deletions src/Mewdeko/Modules/Chat Triggers/Services/ChatTriggersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public enum CtField
private readonly TypedKey<CTModel> crAdded = new("cr.added");
private readonly IBotCredentials creds;
private readonly TypedKey<bool> crsReloadedKey = new("crs.reloaded");
private readonly GeneratedBotStrings Strings;
private readonly GeneratedBotStrings strings;

private readonly DbContextProvider dbProvider;
private readonly DiscordPermOverrideService discordPermOverride;
Expand All @@ -131,7 +131,6 @@ public enum CtField
private readonly PermissionService perms;
private readonly IPubSub pubSub;
private readonly Random rng;
private readonly IBotStrings strings;

// it is perfectly fine to have global chattriggers as an array
// 1. custom reactions are almost never added (compared to how many times they are being looped through)
Expand Down Expand Up @@ -160,7 +159,6 @@ public enum CtField
public ChatTriggersService(
PermissionService perms,
DbContextProvider dbProvider,
IBotStrings strings,
Mewdeko bot,
DiscordShardedClient client,
GlobalPermissionService gperm,
Expand All @@ -169,19 +167,19 @@ public ChatTriggersService(
DiscordPermOverrideService discordPermOverride,
GuildSettingsService guildSettings,
BotConfigService configService,
IBotCredentials creds)
IBotCredentials creds, GeneratedBotStrings strings)
{
this.dbProvider = dbProvider;
this.client = client;
this.perms = perms;
this.strings = strings;
this.cmdCds = cmdCds;
this.gperm = gperm;
this.pubSub = pubSub;
this.discordPermOverride = discordPermOverride;
this.guildSettings = guildSettings;
this.configService = configService;
this.creds = creds;
this.strings = strings;
rng = new MewdekoRandom();

pubSub.Sub(crsReloadedKey, OnCrsShouldReload);
Expand Down Expand Up @@ -254,7 +252,7 @@ public async Task<bool> RunBehavior(DiscordShardedClient socketClient, IGuild gu
{
if (pc.Verbose)
{
var returnMsg = Strings.PermPrevent(guild.Id,
var returnMsg = strings.PermPrevent(guild.Id,
index + 1,
Format.Bold(pc.Permissions[index].GetCommand(await guildSettings.GetPrefix(guild), sg)));
try
Expand Down Expand Up @@ -510,7 +508,7 @@ public async Task RunInteractionTrigger(SocketInteraction inter, CTModel ct, boo
// If verbose mode is enabled, provide a detailed message about the prevented action.
if (!pc.Verbose)
return;
var returnMsg = Strings.PermPrevent(guild.Id,
var returnMsg = strings.PermPrevent(guild.Id,
index + 1,
Format.Bold(pc.Permissions[index]
.GetCommand(await guildSettings.GetPrefix(guild), guild)));
Expand Down Expand Up @@ -1751,59 +1749,98 @@ public async Task ToggleRemovedRole(CTModel ct, ulong rId)
/// <returns>An embed builder containing information about the chat trigger.</returns>
public EmbedBuilder GetEmbed(CTModel ct, ulong? gId = null, string? title = null)
{
var eb = new EmbedBuilder().WithOkColor() // Create a new embed builder
.WithTitle(title) // Set the title
.WithDescription($"#{ct.Id}") // Set the description
.AddField(Strings.CtInteractionTypeTitle(gId), // Add fields for various trigger properties
Strings.CtInteractionTypeBody(gId, ct.ApplicationCommandType.ToString()))
.AddField(Strings.CtRealname(gId), ct.RealName)
.AddField(efb => efb.WithName(Strings.Trigger(gId)).WithValue(ct.Trigger.TrimTo(1024)))
var eb = new EmbedBuilder().WithOkColor()
.WithTitle(title)
.WithDescription($"#{ct.Id}");

try
{
eb.AddField(strings.CtInteractionTypeTitle(gId),
strings.CtInteractionTypeBody(gId, ct.ApplicationCommandType.ToString()));
}
catch
{
eb.AddField(strings.CtInteractionTypeTitle(gId), "Unknown");
}

eb.AddField(strings.CtRealname(gId), ct.RealName ?? "N/A")
.AddField(efb => efb.WithName(strings.Trigger(gId)).WithValue(ct.Trigger?.TrimTo(1024) ?? "N/A"))
.AddField(efb =>
efb.WithName(Strings.Response(gId))
.WithValue($"{(ct.Response + "\n```css\n" + ct.Response).TrimTo(1024 - 11)}```"))
.AddField(Strings.CtPrefixType(gId), ct.PrefixType);
efb.WithName(strings.Response(gId))
.WithValue($"```css\n{(ct.Response ?? "N/A").TrimTo(1024 - 11)}```"))
.AddField(strings.CtPrefixType(gId), ct.PrefixType.ToString());

var reactions = ct.GetReactions(); // Get trigger reactions
if (reactions.Length >= 1)
eb.AddField(Strings.TriggerReactions(gId), string.Concat(reactions)); // Add trigger reactions
try
{
var reactions = ct.GetReactions();
if (reactions is { Length: > 0 })
{
eb.AddField(strings.TriggerReactions(gId), string.Join("", reactions));
}
}
catch (Exception ex)
{
Log.Error(ex, "Error");
}

var addedRoles = ct.GetGrantedRoles(); // Get granted roles
if (addedRoles.Count >= 1)
eb.AddField(Strings.AddedRoles(gId),
addedRoles.Select(x => $"<@&{x}>").Aggregate((x, y) => $"{x}, {y}")); // Add granted roles
try
{
var addedRoles = ct.GetGrantedRoles();
if (addedRoles?.Count > 0)
{
eb.AddField(strings.AddedRoles(gId),
string.Join(", ", addedRoles.Select(x => $"<@&{x}>")));
}

var removedRoles = ct.GetRemovedRoles(); // Get removed roles
if (removedRoles.Count >= 1)
eb.AddField(Strings.RemovedRoles(gId),
removedRoles.Select(x => $"<@&{x}>").Aggregate((x, y) => $"{x}, {y}")); // Add removed roles
var removedRoles = ct.GetRemovedRoles();
if (removedRoles?.Count > 0)
{
eb.AddField(strings.RemovedRoles(gId),
string.Join(", ", removedRoles.Select(x => $"<@&{x}>")));
}

if (addedRoles.Count >= 1 || removedRoles.Count >= 1)
eb.AddField(Strings.RoleGrantType(gId), ct.RoleGrantType); // Add role grant type
if (addedRoles?.Count > 0 || removedRoles?.Count > 0)
{
eb.AddField(strings.RoleGrantType(gId), ct.RoleGrantType.ToString());
}
}
catch (Exception ex)
{
Log.Error(ex, "Error");
}

if (!ct.ApplicationCommandDescription.IsNullOrWhiteSpace())
eb.AddField(Strings.CtInteractionDescription(gId),
ct.ApplicationCommandDescription); // Add interaction description
if (!string.IsNullOrWhiteSpace(ct.ApplicationCommandDescription))
{
eb.AddField(strings.CtInteractionDescription(gId), ct.ApplicationCommandDescription);
}

if (ct.ApplicationCommandId != 0)
eb.AddField(Strings.CtInteractionId(gId),
ct.ApplicationCommandId.ToString()); // Add interaction ID
{
eb.AddField(strings.CtInteractionId(gId), ct.ApplicationCommandId.ToString());
}

if (ct.ValidTriggerTypes != (ChatTriggerType)0b1111)
eb.AddField(Strings.CtValidFields(gId),
ct.ValidTriggerTypes.ToString()); // Add valid trigger types
{
eb.AddField(strings.CtValidFields(gId), ct.ValidTriggerTypes.ToString());
}

if (!ct.CrosspostingWebhookUrl.IsNullOrWhiteSpace())
eb.AddField(Strings.CtCrossposting(gId),
Strings.CtCrosspostingWebhook(gId)); // Add crossposting webhook
if (!string.IsNullOrWhiteSpace(ct.CrosspostingWebhookUrl))
{
eb.AddField(strings.CtCrossposting(gId), strings.CtCrosspostingWebhook(gId));
}

if (ct.CrosspostingChannelId != 0)
eb.AddField(Strings.CtCrossposting(gId),
Strings.CtCrosspostingChannel(gId, ct.CrosspostingChannelId)); // Add crossposting channel
{
eb.AddField(strings.CtCrossposting(gId),
strings.CtCrosspostingChannel(gId, ct.CrosspostingChannelId));
}

if (ct.PrefixType == RequirePrefixType.Custom)
eb.AddField(Strings.CtCustomPrefix(gId), ct.CustomPrefix); // Add custom prefix
if (ct.PrefixType == RequirePrefixType.Custom && !string.IsNullOrWhiteSpace(ct.CustomPrefix))
{
eb.AddField(strings.CtCustomPrefix(gId), ct.CustomPrefix);
}

return eb; // Return the embed builder
return eb;
}

/// <summary>
Expand Down

0 comments on commit 59c2c7b

Please sign in to comment.