forked from darkalfx/requestrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestrrModuleBase.cs
197 lines (169 loc) · 7.24 KB
/
RequestrrModuleBase.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// using System;
// using System.Collections.Generic;
// using System.Threading;
// using System.Threading.Tasks;
// using Discord;
// using Discord.Commands;
// using Discord.WebSocket;
// using Requestrr.WebApi.RequestrrBot.ChatClients.Discord;
// namespace Requestrr.WebApi
// {
// public class RequestrrModuleBase<T> : IDisposable
// {
// private readonly DiscordSocketClient _discord;
// private readonly DiscordSettings _discordSettings;
// private Dictionary<ulong, IReactionCallback> _callbacks = new Dictionary<ulong, IReactionCallback>();
// protected SocketCommandContext Context { get; }
// public RequestrrModuleBase(DiscordSocketClient discord, SocketCommandContext commandContext, DiscordSettingsProvider discordSettingsProvider)
// {
// _discord = discord;
// Context = commandContext;
// _discordSettings = discordSettingsProvider.Provide();
// _discord.ReactionAdded += HandleReactionAsync;
// }
// public void Dispose()
// {
// _discord.ReactionAdded -= HandleReactionAsync;
// }
// protected async Task DeleteSafeAsync(IMessage message, RequestOptions options = null)
// {
// try
// {
// if (message != null)
// {
// if (_discordSettings.AutomaticallyPurgeCommandMessages && !(message.Channel is IPrivateChannel))
// {
// await message.DeleteAsync();
// }
// }
// }
// catch
// {
// //We did our best
// }
// }
// protected async Task ForceDeleteSafeAsync(IMessage message, RequestOptions options = null)
// {
// try
// {
// if (message != null && !(message.Channel is IPrivateChannel))
// {
// await message.DeleteAsync();
// }
// }
// catch
// {
// //We did our best
// }
// }
// protected Task<IUserMessage> ReplyToUserAsync(string message)
// {
// return ReplyAsync($"{Context.Message.Author.Mention} " + message);
// }
// protected async Task<IUserMessage> ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
// {
// return await Context.Channel.SendMessageAsync(message, isTTS, embed, options).ConfigureAwait(false);
// }
// public async Task<SocketReaction> WaitForReactionAsync(SocketCommandContext context, IMessage message, IEmote expectedEmote)
// {
// var token = new CancellationToken();
// var timeout = TimeSpan.FromSeconds(120);
// var eventTrigger = new TaskCompletionSource<SocketReaction>();
// var cancelTrigger = new TaskCompletionSource<bool>();
// token.Register(() => cancelTrigger.SetResult(true));
// var sourceContext = context;
// AddReactionCallback(message, new AsyncReactionCallback(sourceContext, reaction =>
// {
// if (reaction.Emote.Name.Equals(expectedEmote.Name, StringComparison.Ordinal))
// {
// eventTrigger.SetResult(reaction);
// return Task.FromResult(true);
// }
// return Task.FromResult(false);
// }));
// try
// {
// var trigger = eventTrigger.Task;
// var cancel = cancelTrigger.Task;
// var delay = Task.Delay(timeout);
// var task = await Task.WhenAny(trigger, delay, cancel).ConfigureAwait(false);
// if (task == trigger)
// return await trigger.ConfigureAwait(false);
// else
// return null;
// }
// catch
// {
// return null;
// }
// finally
// {
// RemoveReactionCallback(message);
// }
// }
// public void AddReactionCallback(IMessage message, IReactionCallback callback)
// => _callbacks[message.Id] = callback;
// public void RemoveReactionCallback(IMessage message)
// => RemoveReactionCallback(message.Id);
// public void RemoveReactionCallback(ulong id)
// => _callbacks.Remove(id);
// public void ClearReactionCallbacks()
// => _callbacks.Clear();
// private async Task HandleReactionAsync(Cacheable<IUserMessage, ulong> message,
// ISocketMessageChannel channel,
// SocketReaction reaction)
// {
// if (reaction.UserId == _discord.CurrentUser.Id) return;
// if (!(_callbacks.TryGetValue(message.Id, out var callback))) return;
// if (!(callback.Context.Message.Author.Id == reaction.UserId && callback.Context.Channel.Id == reaction.Channel.Id))
// return;
// switch (callback.RunMode)
// {
// case RunMode.Async:
// _ = Task.Run(async () =>
// {
// if (await callback.HandleCallbackAsync(reaction).ConfigureAwait(false))
// RemoveReactionCallback(message.Id);
// });
// break;
// default:
// if (await callback.HandleCallbackAsync(reaction).ConfigureAwait(false))
// RemoveReactionCallback(message.Id);
// break;
// }
// }
// protected async Task<SocketMessage> NextMessageAsync(
// SocketCommandContext context,
// TimeSpan? timeout = null,
// CancellationToken token = default)
// {
// timeout = timeout ?? TimeSpan.FromSeconds(120);
// var eventTrigger = new TaskCompletionSource<SocketMessage>();
// var cancelTrigger = new TaskCompletionSource<bool>();
// token.Register(() => cancelTrigger.SetResult(true));
// var sourceContext = context;
// async Task Handler(SocketMessage message)
// {
// if (sourceContext.Message.Author.Id == message.Author.Id && sourceContext.Channel.Id == message.Channel.Id)
// eventTrigger.SetResult(message);
// }
// try
// {
// context.Client.MessageReceived += Handler;
// var trigger = eventTrigger.Task;
// var cancel = cancelTrigger.Task;
// var delay = Task.Delay(timeout.Value);
// var task = await Task.WhenAny(trigger, delay, cancel).ConfigureAwait(false);
// context.Client.MessageReceived -= Handler;
// if (task == trigger)
// return await trigger.ConfigureAwait(false);
// else
// return null;
// }
// catch
// {
// return null;
// }
// }
// }
// }