Created
December 27, 2021 18:53
-
-
Save GuyInGrey/e56d8475368961e6c009de98b1c130df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using DSharpPlus.Entities; | |
using DSharpPlus.SlashCommands; | |
using DSharpPlus; | |
using DSharpPlus.EventArgs; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
using System; | |
namespace IcarusPrototype.Commands | |
{ | |
public class ModCommands : ApplicationCommandModule | |
{ | |
static List<Mute> Mutes = new(); | |
DiscordBot Bot; | |
JObject Snowflakes; | |
public ModCommands(DiscordBot bot, JObject config) | |
{ | |
Snowflakes = (JObject)config["snowflakes"]; | |
Bot = bot; | |
bot.Discord.ComponentInteractionCreated += Discord_ComponentInteractionCreated; | |
} | |
private async Task Discord_ComponentInteractionCreated(DiscordClient sender, ComponentInteractionCreateEventArgs e) | |
{ | |
await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage); | |
if (e.Id == "toggleServer") | |
{ | |
var mute = Mutes.First(m => m.ActiveLogMessageID == e.Message.Id); | |
mute.ServerAccess = !mute.ServerAccess; | |
await ControlRoles(mute); | |
var embed = BuildEmbed(mute, new DiscordEmbedBuilder()); | |
await e.Message.ModifyAsync(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(Buttons(mute))); | |
} | |
else if (e.Id == "unmute") | |
{ | |
var mute = Mutes.First(m => m.ActiveLogMessageID == e.Message.Id); | |
mute.ServerAccess = true; | |
await ControlRoles(mute); | |
var modlogs = await Bot.Discord.GetChannelAsync(Snowflake("channels.mod-logs")); | |
await modlogs .SendMessageAsync(new DiscordEmbedBuilder() | |
.WithColor(DiscordColor.CornflowerBlue) | |
.WithTitle("Member Was Muted") | |
.AddField("Member", "<@" + mute.UserID + ">") | |
.AddField("Reason", mute.Reason) | |
.AddField("Thread", "<#" + mute.ThreadID + ">") | |
.AddField("Muted By", "<@" + mute.MuterID + ">") | |
.WithAuthor(Bot.Discord.CurrentUser.Username, null, Bot.Discord.CurrentUser.AvatarUrl) | |
.Build()); | |
var thread = await Bot.Discord.GetChannelAsync(mute.ThreadID) as DiscordThreadChannel; | |
await e.Message.DeleteAsync(); | |
} | |
} | |
[SlashCommand("mute2", "Mute a member.")] | |
public async Task MuteCommand(InteractionContext ctx, | |
[Option("user", "User to mute.")] DiscordUser toMute, | |
[Option("Reason", "The reason the user is muted.")] string reason, | |
[Option("serveraccess", "Allow the user to send/read in the rest of the server?")] bool serverAccess = false) | |
{ | |
await ctx.DeferAsync(); | |
var m = new Mute() | |
{ | |
UserID = toMute.Id, | |
ServerAccess = serverAccess, | |
Reason = reason, | |
MuterID = ctx.User.Id, | |
}; | |
Mutes.Add(m); | |
var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); | |
var muteChannel = await ctx.Client.GetChannelAsync(Snowflake("channels.mute-channel")); | |
var t = await muteChannel.CreateThreadAsync("mute-" + timestamp, AutoArchiveDuration.Day, | |
ChannelType.PublicThread, "Muted."); | |
m.ThreadID = t.Id; | |
var guild = await Bot.Discord.GetGuildAsync(Snowflake("guild")); | |
var member = await guild.GetMemberAsync(toMute.Id); | |
await t.AddThreadMemberAsync(member); | |
var e = BuildEmbed(m, new DiscordEmbedBuilder()); | |
var modActive = await ctx.Client.GetChannelAsync(Snowflake("channels.mod-active")); | |
var logMessage = await modActive.SendMessageAsync(new DiscordMessageBuilder().WithEmbed(e).AddComponents(Buttons(m))); | |
m.ActiveLogMessageID = logMessage.Id; | |
await ControlRoles(m); | |
await t.SendMessageAsync(toMute.Mention + ", you have been muted."); | |
await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Muted " + toMute)); | |
} | |
public DiscordEmbedBuilder BuildEmbed(Mute m, DiscordEmbedBuilder b) | |
{ | |
b = b.WithColor(DiscordColor.Red) | |
.WithTitle("Member Mute") | |
.AddField("Member", "<@" + m.UserID + ">") | |
.AddField("Reason", m.Reason) | |
.AddField("Thread", "<#" + m.ThreadID + ">") | |
.AddField("Server Access", m.ServerAccess ? "Yes" : "No") | |
.AddField("Muted By", "<@" + m.MuterID + ">") | |
.WithAuthor(Bot.Discord.CurrentUser.Username, null, Bot.Discord.CurrentUser.AvatarUrl); | |
return b; | |
} | |
public static DiscordComponent[] Buttons(Mute m) | |
{ | |
return new DiscordComponent[] | |
{ | |
new DiscordButtonComponent(ButtonStyle.Success, "unmute", "Unmute"), | |
new DiscordButtonComponent(m.ServerAccess ? ButtonStyle.Danger : ButtonStyle.Primary, | |
"toggleServer", m.ServerAccess ? "Disable Server Access" : "Enable Server Access"), | |
}; | |
} | |
public async Task ControlRoles(Mute m) | |
{ | |
var mutedRoleID = Snowflake("roles.muted"); | |
var guild = await Bot.Discord.GetGuildAsync(Snowflake("guild")); | |
var member = await guild.GetMemberAsync(m.UserID); | |
var mutedRole = guild.GetRole(mutedRoleID); | |
if (m.ServerAccess) | |
{ | |
if (member.Roles.Any(r => r.Id == mutedRoleID)) | |
{ | |
await member.RevokeRoleAsync(mutedRole); | |
} | |
} | |
else | |
{ | |
if (!member.Roles.Any(r => r.Id == mutedRoleID)) | |
{ | |
await member.GrantRoleAsync(mutedRole); | |
} | |
} | |
} | |
public ulong Snowflake(string path) | |
{ | |
return Snowflakes.SelectToken(path).Value<ulong>(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace IcarusPrototype.Commands | |
{ | |
public class Mute | |
{ | |
public ulong ActiveLogMessageID; | |
public ulong UserID; | |
public ulong ThreadID; | |
public bool ServerAccess; | |
public string Reason; | |
public ulong MuterID; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment