forked from PizzaConsole/DiscordCopyBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.cs
74 lines (67 loc) · 3.32 KB
/
Utilities.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
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using DSharpPlus.Interactivity.Extensions;
using System.Threading.Tasks;
namespace ControlCopy {
public static class Utilities {
public delegate IEnumerable<DiscordChannel> Filter(CommandContext ctx);
public delegate IEnumerable<DiscordChannel> FilterChannel(DiscordChannel cat);
public delegate bool Binder();
public delegate Task<bool> AsyncBinder();
// public delegate void Printer(CommandContext ctx, string s);
public static void executeFor(Func<bool> func, TimeSpan timeSpan) {
Stopwatch s = Stopwatch.StartNew();
while (s.ElapsedMilliseconds < timeSpan.TotalMilliseconds && !func());
// func();
}
public static async Task asyncExecuteFor(Func<Task<bool>> func, TimeSpan timeSpan) {
Stopwatch s = Stopwatch.StartNew();
while (s.ElapsedMilliseconds < timeSpan.TotalMilliseconds && !(await func()));
}
// public static Printer send = (ctx, s) => {
// ctx.Channel.SendMessageAsync(s);
// };
public enum PolarReactionState {Yes, No, TimedOut}
public static async Task<PolarReactionState> polarReaction(CommandContext ctx, DiscordMessage mes) {
await mes.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":white_check_mark:"));
await mes.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":x:"));
// var collectedReactions = await mes.CollectReactionsAsync(TimeSpan.FromSeconds(60));
// System.Collections.ObjectModel.ReadOnlyCollection<DSharpPlus.Interactivity.EventHandling.Reaction> collectedReactions;
System.Collections.ObjectModel.ReadOnlyCollection<DSharpPlus.Interactivity.EventHandling.Reaction> collectedReactions;
IEnumerable<DSharpPlus.Interactivity.EventHandling.Reaction> result = null;
AsyncBinder checkReaction = async () => {
collectedReactions = await mes.CollectReactionsAsync(TimeSpan.FromMilliseconds(500));
result = collectedReactions.Where(r => new string[]{":white_check_mark:", ":x:"}.Contains(r.Emoji.GetDiscordName()) && r.Users.Contains(ctx.Message.Author));
return result.Count() > 0;
};
await asyncExecuteFor(() => checkReaction(), TimeSpan.FromSeconds(60));
if (result != null)
{
if (result.FirstOrDefault(r => r.Emoji.GetDiscordName() == ":white_check_mark:") != null)
return PolarReactionState.Yes;
else
return PolarReactionState.No;
}
else
return PolarReactionState.TimedOut;
}
public static Filter allSupportedChannels = (ctx) => {
var firstFiltering = ctx.Guild.Channels.Select(e => e.Value).Where(c => !(new ChannelType[]{ChannelType.Voice, ChannelType.Unknown}.Contains(c.Type)));
var secondFiltering = firstFiltering.Except(
firstFiltering.Where(c => c.Type == ChannelType.Category)
.Where(c => c.Children.All(s => !firstFiltering.Contains(s)))
);
return secondFiltering;
};
public static FilterChannel onlySupported = (cat) => {
return cat.Children.Where(c => !(new ChannelType[]{ChannelType.Voice, ChannelType.Unknown}.Contains(c.Type)));
};
public static Predicate<DiscordMember> isMemberDisallowed = (m) => m.Roles.Where(r => r.Permissions.HasPermission(Permissions.Administrator) || r.Name == "Archivist").Count() == 0 && !m.IsOwner;
}
}