forked from drizzle-mizzle/CharacterAI-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandsServiceChannels.cs
76 lines (63 loc) · 3.3 KB
/
CommandsServiceChannels.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
73
74
75
76
using CharacterAI;
using Discord.Commands;
using Discord.Rest;
using Discord;
using Discord.WebSocket;
using CharacterAI_Discord_Bot.Handlers;
using CharacterAI_Discord_Bot.Models;
namespace CharacterAI_Discord_Bot.Service
{
/// <summary>
/// Separate partial class file for a static methods working with channels.
/// </summary>
public partial class CommandsService : CommonService
{
internal static void DeactivateOldUserChannel(CommandsHandler handler, ulong userId, ulong guildId)
{
var userChannels = handler.Channels.Where(c => c.ChannelAuthorId == userId && c.GuildId == guildId);
var newChannelsList = new List<DiscordChannel>();
newChannelsList.AddRange(handler.Channels); // add all channels
foreach (var userChannel in userChannels)
{
var channelToRemove = handler.Channels.Find(c => c.ChannelId == userChannel.ChannelId);
if (channelToRemove is null) continue;
newChannelsList.Remove(channelToRemove); // delete old channel
}
// Update channels list with a new list without old channels
handler.Channels = newChannelsList;
}
internal static async Task<ulong> FindOrCreateCategoryAsync(SocketCommandContext context, string categoryName)
{
// Find category on server by its name.
var category = context.Guild.CategoryChannels.FirstOrDefault(c => c.Name == categoryName);
if (category is not null) return category.Id;
// Create category if it does not exist.
var newCategory = await context.Guild.CreateCategoryChannelAsync(categoryName, c =>
c.PermissionOverwrites = new List<Overwrite>() { ViewChannelPermOverwrite(context.Guild.EveryoneRole, PermValue.Allow) }
);
return newCategory.Id;
}
internal static async Task<RestTextChannel> CreateChannelAsync(SocketCommandContext context, ulong categoryId, Integration cI)
{
string channelName = $"private chat with {cI.CurrentCharacter.Name}";
var category = context.Guild.GetCategoryChannel(categoryId);
var catPerms = new List<Overwrite>() { ViewChannelPermOverwrite(context.Message.Author, PermValue.Allow) };
await category.ModifyAsync(c => c.PermissionOverwrites = catPerms);
var channelPerms = new List<Overwrite>()
{
ViewChannelPermOverwrite(context.Message.Author, PermValue.Allow),
ViewChannelPermOverwrite(context.Client.CurrentUser, PermValue.Allow),
ViewChannelPermOverwrite(context.Guild.EveryoneRole, PermValue.Deny)
};
var newChannel = await context.Guild.CreateTextChannelAsync(channelName, c
=> { c.CategoryId = categoryId; c.PermissionOverwrites = channelPerms; });
return newChannel;
}
public static Overwrite ViewChannelPermOverwrite(ISnowflakeEntity target, PermValue permValue)
{
PermissionTarget permTarget = target is SocketUser ? PermissionTarget.User : PermissionTarget.Role;
var permission = new OverwritePermissions(viewChannel: permValue);
return new Overwrite(target.Id, permTarget, permission);
}
}
}