-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathban.cs
More file actions
132 lines (113 loc) · 5.29 KB
/
ban.cs
File metadata and controls
132 lines (113 loc) · 5.29 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
using System;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace Moderation_Bot.commands
{
public class BanCommands : BaseCommandModule
{
private bool HasBanPermission(CommandContext ctx) =>
ctx.Member.PermissionsIn(ctx.Channel).HasPermission(Permissions.BanMembers);
private bool CanBeBanned(CommandContext ctx, DiscordMember target) =>
ctx.Member.Hierarchy > target.Hierarchy && !target.IsBot;
private async Task RespondEmbed(CommandContext ctx, string title, string description, DiscordColor? color = null)
{
var embed = new DiscordEmbedBuilder
{
Title = title,
Description = description,
Color = color ?? DiscordColor.Red
};
await ctx.RespondAsync(embed: embed);
}
[Command("ban")]
[Description("Bans a user for a specified duration (days or 'perm') and reason. Usage: .ban @user 7, Spamming")]
public async Task Ban(CommandContext ctx, DiscordMember target, [RemainingText] string args)
{
await HandleBan(ctx, target, args, "ban");
}
[Command("banish")]
[Description("Bans a user for a specified duration (days or 'perm') and reason. Usage: .banish @user perm, Rule violation")]
public async Task Banish(CommandContext ctx, DiscordMember target, [RemainingText] string args)
{
await HandleBan(ctx, target, args, "banish");
}
private async Task HandleBan(CommandContext ctx, DiscordMember target, string args, string commandName)
{
if (!HasBanPermission(ctx))
{
await RespondEmbed(ctx, "Insufficient Permissions", "You do not have permission to ban members.");
return;
}
if (!CanBeBanned(ctx, target))
{
await RespondEmbed(ctx, "Cannot Ban", "You cannot ban this user.");
return;
}
if (string.IsNullOrWhiteSpace(args) || !args.Contains(","))
{
await RespondEmbed(ctx, "Invalid Usage", $"Usage: .{commandName} @user <days|perm>, <reason>");
return;
}
var split = args.Split(new[] { ',' }, 2, StringSplitOptions.RemoveEmptyEntries);
var durationInput = split[0].Trim();
var reason = split.Length > 1 ? split[1].Trim() : "No reason provided";
int deleteDays = 0;
if (durationInput.Equals("perm", StringComparison.OrdinalIgnoreCase))
{
deleteDays = 7; // Discord's max days for message deletion
}
else if (!int.TryParse(durationInput, out deleteDays) || deleteDays < 0 || deleteDays > 7)
{
await RespondEmbed(ctx, "Invalid Duration", "Duration must be a number of days (0-7) or 'perm' for permanent.");
return;
}
await target.BanAsync(deleteDays, reason);
var embed = new DiscordEmbedBuilder
{
Title = "User Banned",
Description = $"{target.Mention} has been banned.\nDuration: {(durationInput.Equals("perm", StringComparison.OrdinalIgnoreCase) ? "Permanent" : $"{deleteDays} day(s) of message deletion")}\nReason: {reason}",
Color = DiscordColor.Orange
};
await ctx.RespondAsync(embed: embed);
}
[Command("unban")]
[Description("Unbans a user by their user ID. Usage: .unban <userId> [reason]")]
public async Task Unban(CommandContext ctx, ulong userId, [RemainingText] string reason = "No reason provided")
{
await HandleUnban(ctx, userId, reason, "unban");
}
[Command("unbanish")]
[Description("Unbans a user by their user ID. Usage: .unbanish <userId> [reason]")]
public async Task Unbanish(CommandContext ctx, ulong userId, [RemainingText] string reason = "No reason provided")
{
await HandleUnban(ctx, userId, reason, "unbanish");
}
private async Task HandleUnban(CommandContext ctx, ulong userId, string reason, string commandName)
{
if (!HasBanPermission(ctx))
{
await RespondEmbed(ctx, "Insufficient Permissions", "You do not have permission to unban members.");
return;
}
var bans = await ctx.Guild.GetBansAsync();
var ban = bans.FirstOrDefault(b => b.User.Id == userId);
if (ban == null)
{
await RespondEmbed(ctx, "User Not Banned", $"No banned user with ID `{userId}` found.");
return;
}
await ctx.Guild.UnbanMemberAsync(userId, reason);
var embed = new DiscordEmbedBuilder
{
Title = "User Unbanned",
Description = $"User with ID `{userId}` has been unbanned.\nReason: {reason}",
Color = DiscordColor.Green
};
await ctx.RespondAsync(embed: embed);
}
}
}