-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommandHelpers.cs
72 lines (61 loc) · 2.38 KB
/
CommandHelpers.cs
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
using System;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
namespace RoboBot
{
public static class CommandHelpers
{
public static async Task<bool> CheckPermissions(CommandContext ctx, Permissions requiredPermissions)
{
DiscordMember member = (DiscordMember)ctx.User;
if (!member.PermissionsIn(ctx.Channel).HasPermission(requiredPermissions))
{
await ctx.RespondAsync($"You don't have the required permissions to execute this command\nYou need {requiredPermissions.ToPermissionString()}");
return false;
}
return true;
}
public static async Task<DiscordMessage> GetMessageFromUrl(CommandContext ctx, string messageUrl)
{
if (!Uri.TryCreate(messageUrl, UriKind.Absolute, out Uri messageUri))
{
await ctx.RespondAsync("Bad message link, try again");
return null;
}
string[] urlParts = messageUri.PathAndQuery.Remove(0, 1).Split('/');
if (urlParts.Length != 4)
{
await ctx.RespondAsync("Bad message link, try again");
return null;
}
if(!ulong.TryParse(urlParts[2], out ulong channelId) || !ulong.TryParse(urlParts[3], out ulong messageId))
{
await ctx.RespondAsync("Bad message link, try again");
return null;
}
if (!ctx.Guild.Channels.TryGetValue(channelId, out DiscordChannel channel))
{
await ctx.RespondAsync("Can't get the channel, make sure it is part of this Discord server and that the bot has access to it");
return null;
}
DiscordMessage message;
try
{
message = await channel.GetMessageAsync(messageId);
}
catch (Exception e)
{
await ctx.RespondAsync(e.ToString());
return null;
}
if (message is null)
{
await ctx.RespondAsync("Could not get the message, make sure it is in the same server and that the bot has access to it");
return null;
}
return message;
}
}
}