Skip to content

Commit 65bd3b9

Browse files
committed
feat: client extensions
1 parent 1950194 commit 65bd3b9

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace DiscordLab.Bot.API.Enums
2+
{
3+
/// <summary>
4+
/// Used to determine if a channel was found or not, and if not, the reason why it wasn't found.
5+
/// </summary>
6+
public enum ChannelReturn
7+
{
8+
/// <summary>
9+
/// The guild and channel were found.
10+
/// </summary>
11+
Found = 0,
12+
/// <summary>
13+
/// Couldn't find the guild with the specified ID.
14+
/// </summary>
15+
InvalidGuild = 1,
16+
/// <summary>
17+
/// The guild ID was 0.
18+
/// </summary>
19+
NoGuild = 2,
20+
/// <summary>
21+
/// Couldn't find the channel with the specified ID.
22+
/// </summary>
23+
InvalidChannel = 3,
24+
/// <summary>
25+
/// The channel ID was 0.
26+
/// </summary>
27+
NoChannel = 4,
28+
/// <summary>
29+
/// The channel type was not the requested type.
30+
/// </summary>
31+
InvalidType = 5
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace DiscordLab.Bot.API.Enums
2+
{
3+
/// <summary>
4+
/// Used to determine if a guild was found or not, and if not, the reason why it wasn't found.
5+
/// </summary>
6+
public enum GuildReturn
7+
{
8+
/// <summary>
9+
/// The guild was found.
10+
/// </summary>
11+
Found = 0,
12+
/// <summary>
13+
/// The guild ID was invalid.
14+
/// </summary>
15+
InvalidGuild = 1,
16+
/// <summary>
17+
/// The guild ID was 0.
18+
/// </summary>
19+
NoGuild = 2
20+
}
21+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Discord.WebSocket;
2+
using DiscordLab.Bot.API.Enums;
3+
4+
namespace DiscordLab.Bot.API.Extensions
5+
{
6+
public static class ClientExtensions
7+
{
8+
public static GuildReturn TryGetGuild(this DiscordSocketClient client, ulong guildId, out SocketGuild guild)
9+
{
10+
if (guildId == 0)
11+
{
12+
guild = null;
13+
return GuildReturn.NoGuild;
14+
}
15+
SocketGuild tempGuild = client.GetGuild(guildId);
16+
if (tempGuild == null)
17+
{
18+
guild = null;
19+
return GuildReturn.InvalidGuild;
20+
}
21+
guild = tempGuild;
22+
return GuildReturn.Found;
23+
}
24+
25+
public static ChannelReturn TryGetTextChannel(this DiscordSocketClient client, ulong channelId, out SocketTextChannel channel)
26+
{
27+
if (channelId == 0)
28+
{
29+
channel = null;
30+
return ChannelReturn.NoChannel;
31+
}
32+
SocketChannel tempChannel = client.GetChannel(channelId);
33+
if (tempChannel == null)
34+
{
35+
channel = null;
36+
return ChannelReturn.InvalidChannel;
37+
}
38+
if(tempChannel is not SocketTextChannel textChannel)
39+
{
40+
channel = null;
41+
return ChannelReturn.InvalidType;
42+
}
43+
channel = textChannel;
44+
return ChannelReturn.Found;
45+
}
46+
47+
public static ChannelReturn TryGetTextChannel(this SocketGuild guild, ulong channelId,
48+
out SocketTextChannel channel)
49+
{
50+
if(channelId == 0)
51+
{
52+
channel = null;
53+
return ChannelReturn.NoChannel;
54+
}
55+
SocketTextChannel tempChannel = guild.GetTextChannel(channelId);
56+
if (tempChannel == null)
57+
{
58+
channel = null;
59+
return ChannelReturn.InvalidChannel;
60+
}
61+
channel = tempChannel;
62+
return ChannelReturn.Found;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)