-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout.cs
More file actions
133 lines (114 loc) · 5.3 KB
/
timeout.cs
File metadata and controls
133 lines (114 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace Moderation_Bot.commands
{
public class TimeoutCommands : BaseCommandModule
{
// Helper method to check permissions and mute status
private bool HasMutePermission(CommandContext ctx) =>
ctx.Member.PermissionsIn(ctx.Channel).HasPermission(Permissions.MuteMembers);
private bool CanBeMuted(CommandContext ctx, DiscordMember target) =>
ctx.Member.Hierarchy > target.Hierarchy && !target.IsBot;
private bool IsAlreadyMuted(DiscordMember target) =>
target.CommunicationDisabledUntil.HasValue && target.CommunicationDisabledUntil > DateTimeOffset.UtcNow;
private async Task RespondEmbed(CommandContext ctx, string title, string description)
{
var embed = new DiscordEmbedBuilder
{
Title = title,
Description = description,
Color = DiscordColor.Red
};
await ctx.RespondAsync(embed: embed);
}
[Command("mute")]
[Description("Timeouts a user for a specified duration and reason.")]
public async Task Mute(CommandContext ctx, DiscordMember target, TimeSpan duration, [RemainingText] string reason = "No reason provided")
{
await HandleTimeout(ctx, target, duration, reason, "mute");
}
[Command("timeout")]
[Description("Timeouts a user for a specified duration and reason.")]
public async Task Timeout(CommandContext ctx, DiscordMember target, TimeSpan duration, [RemainingText] string reason = "No reason provided")
{
await HandleTimeout(ctx, target, duration, reason, "timeout");
}
[Command("shut")]
[Description("Timeouts a user for a specified duration and reason.")]
public async Task Shut(CommandContext ctx, DiscordMember target, TimeSpan duration, [RemainingText] string reason = "No reason provided")
{
await HandleTimeout(ctx, target, duration, reason, "shut");
}
[Command("stfu")]
[Description("Timeouts a user for a specified duration and reason.")]
public async Task Stfu(CommandContext ctx, DiscordMember target, TimeSpan duration, [RemainingText] string reason = "No reason provided")
{
await HandleTimeout(ctx, target, duration, reason, "stfu");
}
private async Task HandleTimeout(CommandContext ctx, DiscordMember target, TimeSpan duration, string reason, string commandName)
{
if (IsAlreadyMuted(target))
{
await RespondEmbed(ctx, "Already Muted", $"{target.Mention} is already muted.");
return;
}
if (!HasMutePermission(ctx))
{
await RespondEmbed(ctx, "Insufficient Permissions", "You do not have permission to mute members.");
return;
}
if (!CanBeMuted(ctx, target))
{
await RespondEmbed(ctx, "Cannot Mute", "You cannot mute this user.");
return;
}
// Convert TimeSpan to DateTimeOffset by adding it to the current UTC time
var timeoutUntil = DateTimeOffset.UtcNow.Add(duration);
await target.TimeoutAsync(timeoutUntil, reason);
var embed = new DiscordEmbedBuilder
{
Title = "User Muted",
Description = $"{target.Mention} has been muted for {duration:g}.\nReason: {reason}",
Color = DiscordColor.Green
};
await ctx.RespondAsync(embed: embed);
}
[Command("unmute")]
[Description("Removes timeout from a user.")]
public async Task Unmute(CommandContext ctx, DiscordMember target, [RemainingText] string reason = "No reason provided")
{
await HandleUnmute(ctx, target, reason, "unmute");
}
[Command("unshut")]
[Description("Removes timeout from a user.")]
public async Task Unshut(CommandContext ctx, DiscordMember target, [RemainingText] string reason = "No reason provided")
{
await HandleUnmute(ctx, target, reason, "unshut");
}
private async Task HandleUnmute(CommandContext ctx, DiscordMember target, string reason, string commandName)
{
if (!HasMutePermission(ctx))
{
await RespondEmbed(ctx, "Insufficient Permissions", "You do not have permission to unmute members.");
return;
}
if (!IsAlreadyMuted(target))
{
await RespondEmbed(ctx, "Not Muted", $"{target.Mention} is not currently muted.");
return;
}
await target.TimeoutAsync(null, reason);
var embed = new DiscordEmbedBuilder
{
Title = "User Unmuted",
Description = $"{target.Mention} has been unmuted.\nReason: {reason}",
Color = DiscordColor.Green
};
await ctx.RespondAsync(embed: embed);
}
}
}