Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 4052fd8

Browse files
committed
Updated to Discord.net 3.4 for api V9. Also had to update the interactive addon to another fork. Should really switch interactive plugins fully.
1 parent af6a743 commit 4052fd8

25 files changed

+821
-5
lines changed

BotHATTwaffle2.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotHATTwaffle2", "BotHATTwa
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreRCON", "CoreRCON\CoreRCON.csproj", "{4ADEAD8F-818B-4061-A906-A3F5F7B2FDA5}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Discord.Addons.Interactive", "Discord.Addons.Interactive\Discord.Addons.Interactive.csproj", "{6BC829F6-D65A-4D30-B9A1-7E0A6BBC292E}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{4ADEAD8F-818B-4061-A906-A3F5F7B2FDA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{4ADEAD8F-818B-4061-A906-A3F5F7B2FDA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{4ADEAD8F-818B-4061-A906-A3F5F7B2FDA5}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{6BC829F6-D65A-4D30-B9A1-7E0A6BBC292E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{6BC829F6-D65A-4D30-B9A1-7E0A6BBC292E}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{6BC829F6-D65A-4D30-B9A1-7E0A6BBC292E}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{6BC829F6-D65A-4D30-B9A1-7E0A6BBC292E}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

BotHATTwaffle2/BotHATTwaffle2.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Discord.Extensions.Interactive" Version="2.3.0" />
16-
<PackageReference Include="Discord.Net" Version="2.3.1" />
15+
<PackageReference Include="Discord.Net" Version="3.4.1" />
1716
<PackageReference Include="FaceitLib" Version="1.0.7" />
1817
<PackageReference Include="FluentFTP" Version="25.0.3" />
1918
<PackageReference Include="FluentScheduler" Version="5.3.0" />
@@ -24,7 +23,7 @@
2423
<PackageReference Include="Imgur.API" Version="4.0.1" />
2524
<PackageReference Include="LiteDB" Version="4.1.4" />
2625
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
27-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
26+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2827
<PackageReference Include="Pfim" Version="0.8.0" />
2928
<PackageReference Include="Renci.SshNet.Async" Version="1.4.0" />
3029
<PackageReference Include="SharpZipLib" Version="1.2.0" />
@@ -36,6 +35,7 @@
3635

3736
<ItemGroup>
3837
<ProjectReference Include="..\CoreRCON\CoreRCON.csproj" />
38+
<ProjectReference Include="..\Discord.Addons.Interactive\Discord.Addons.Interactive.csproj" />
3939
</ItemGroup>
4040

4141
<ItemGroup>

BotHATTwaffle2/src/Handlers/UserJoinHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ await _log.LogMessage($"USER JOINED {user}" +
5151
//DatabaseUtil.AddJoinedUser(user.Id);
5252
}
5353

54-
// private async Task UserLeftEventHandler(SocketGuildUser user)
54+
// private async Task UserLeftEventHandler(SocketUser user)
5555
// {
5656
//
5757
// }

BotHATTwaffle2/src/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static async Task Main(string[] args)
3030
//Always download users to make sure we can always get them
3131
var config = new DiscordSocketConfig
3232
{
33-
AlwaysDownloadUsers = true
33+
AlwaysDownloadUsers = true,
34+
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers
3435
};
3536

3637
var interactiveConfig = new InteractiveServiceConfig
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Discord.Commands;
4+
using Discord.WebSocket;
5+
6+
namespace Discord.Addons.Interactive
7+
{
8+
public interface IReactionCallback
9+
{
10+
RunMode RunMode { get; }
11+
ICriterion<SocketReaction> Criterion { get; }
12+
TimeSpan? Timeout { get; }
13+
SocketCommandContext Context { get; }
14+
15+
Task<bool> HandleCallbackAsync(SocketReaction reaction);
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Discord.Commands;
4+
5+
namespace Discord.Addons.Interactive
6+
{
7+
public class Criteria<T> : ICriterion<T>
8+
{
9+
private List<ICriterion<T>> _critiera = new List<ICriterion<T>>();
10+
11+
public Criteria<T> AddCriterion(ICriterion<T> criterion)
12+
{
13+
_critiera.Add(criterion);
14+
return this;
15+
}
16+
17+
public async Task<bool> JudgeAsync(SocketCommandContext sourceContext, T parameter)
18+
{
19+
foreach (var criterion in _critiera)
20+
{
21+
var result = await criterion.JudgeAsync(sourceContext, parameter).ConfigureAwait(false);
22+
if (!result) return false;
23+
}
24+
return true;
25+
}
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Discord.Commands;
6+
using Discord.WebSocket;
7+
8+
namespace Discord.Addons.Interactive
9+
{
10+
public class EmptyCriterion<T> : ICriterion<T>
11+
{
12+
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, T parameter)
13+
=> Task.FromResult(true);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using Discord.Commands;
3+
4+
namespace Discord.Addons.Interactive
5+
{
6+
public class EnsureFromChannelCriterion : ICriterion<IMessage>
7+
{
8+
private readonly ulong _channelId;
9+
10+
public EnsureFromChannelCriterion(IMessageChannel channel)
11+
=> _channelId = channel.Id;
12+
13+
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, IMessage parameter)
14+
{
15+
bool ok = _channelId == parameter.Channel.Id;
16+
return Task.FromResult(ok);
17+
}
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.ComponentModel;
2+
using System.Threading.Tasks;
3+
using Discord.Commands;
4+
5+
namespace Discord.Addons.Interactive
6+
{
7+
public class EnsureFromUserCriterion : ICriterion<IMessage>
8+
{
9+
private readonly ulong _id;
10+
11+
public EnsureFromUserCriterion(IUser user)
12+
=> _id = user.Id;
13+
14+
[EditorBrowsable(EditorBrowsableState.Never)]
15+
public EnsureFromUserCriterion(ulong id)
16+
=> _id = id;
17+
18+
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, IMessage parameter)
19+
{
20+
bool ok = _id == parameter.Author.Id;
21+
return Task.FromResult(ok);
22+
}
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Threading.Tasks;
2+
using Discord.Commands;
3+
4+
namespace Discord.Addons.Interactive
5+
{
6+
public class EnsureSourceChannelCriterion : ICriterion<IMessage>
7+
{
8+
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, IMessage parameter)
9+
{
10+
var ok = sourceContext.Channel.Id == parameter.Channel.Id;
11+
return Task.FromResult(ok);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Threading.Tasks;
2+
using Discord.Commands;
3+
4+
namespace Discord.Addons.Interactive
5+
{
6+
public class EnsureSourceUserCriterion : ICriterion<IMessage>
7+
{
8+
public Task<bool> JudgeAsync(SocketCommandContext sourceContext, IMessage parameter)
9+
{
10+
var ok = sourceContext.User.Id == parameter.Author.Id;
11+
return Task.FromResult(ok);
12+
}
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Discord.WebSocket;
5+
using System.Threading.Tasks;
6+
using Discord.Commands;
7+
8+
namespace Discord.Addons.Interactive
9+
{
10+
public interface ICriterion<in T>
11+
{
12+
Task<bool> JudgeAsync(SocketCommandContext sourceContext, T parameter);
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Authors>foxbot</Authors>
5+
6+
<PackageId>Discord.Addons.Interactive</PackageId>
7+
<PackageLicenseUrl>https://opensource.org/licenses/isc</PackageLicenseUrl>
8+
<PackageProjectUrl>https://github.com/foxbot/Discord.Addons.Interactive</PackageProjectUrl>
9+
<PackageTags>discord;discord.net</PackageTags>
10+
<RepositoryType>git</RepositoryType>
11+
<RepositoryUrl>git://github.com/foxbot/Discord.Addons.Interactive</RepositoryUrl>
12+
<PackageIconUrl>https://github.com/foxbot/Discord.Addons.Interactive/raw/master/marketing/PackageLogo.png</PackageIconUrl>
13+
14+
<VersionPrefix Condition=" '$(Version)' != '' ">$(Version)</VersionPrefix>
15+
<VersionPrefix Condition=" '$(Version)' == '' ">0.0.0-dev</VersionPrefix>
16+
17+
<TargetFramework>netstandard2.0</TargetFramework>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<PackageReference Include="Discord.Net.Commands" Version="3.3.2" />
22+
<PackageReference Include="Discord.Net.WebSocket" Version="3.3.2" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Discord.Commands;
2+
using Discord.WebSocket;
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Discord.Addons.Interactive
9+
{
10+
public class InlineReactionCallback : IReactionCallback
11+
{
12+
public RunMode RunMode => RunMode.Async;
13+
14+
public ICriterion<SocketReaction> Criterion { get; }
15+
16+
public TimeSpan? Timeout { get; }
17+
18+
public SocketCommandContext Context { get; }
19+
20+
public IUserMessage Message { get; private set; }
21+
22+
private readonly InteractiveService _interactive;
23+
private readonly ReactionCallbackData _data;
24+
readonly ConcurrentDictionary<ulong, DateTime> _cooldowns = new ConcurrentDictionary<ulong, DateTime>();
25+
26+
public InlineReactionCallback(
27+
InteractiveService interactive,
28+
SocketCommandContext context,
29+
ReactionCallbackData data,
30+
ICriterion<SocketReaction> criterion = null)
31+
{
32+
_interactive = interactive;
33+
Context = context;
34+
_data = data;
35+
Criterion = criterion ?? new EmptyCriterion<SocketReaction>();
36+
Timeout = data.Timeout;
37+
}
38+
39+
public async Task DisplayAsync()
40+
{
41+
var message = await Context.Channel.SendMessageAsync(_data.Text, embed: _data.Embed).ConfigureAwait(false);
42+
Message = message;
43+
_interactive.AddReactionCallback(message, this);
44+
45+
_ = Task.Run(async () =>
46+
{
47+
foreach (var item in _data.Callbacks)
48+
await message.AddReactionAsync(item.Reaction);
49+
});
50+
51+
if (Timeout.HasValue)
52+
{
53+
_ = Task.Delay(Timeout.Value)
54+
.ContinueWith(_ => _interactive.RemoveReactionCallback(message));
55+
}
56+
}
57+
58+
public async Task<bool> HandleCallbackAsync(SocketReaction reaction)
59+
{
60+
var reactionCallbackItem = _data.Callbacks.FirstOrDefault(t => t.Reaction.Equals(reaction.Emote));
61+
if (reactionCallbackItem == null)
62+
return false;
63+
64+
if (_data.RemoveReaction)
65+
{
66+
if (reaction.Message.IsSpecified && reaction.User.IsSpecified)
67+
await reaction.Message.Value.RemoveReactionAsync(reaction.Emote, reaction.User.Value);
68+
}
69+
70+
if (_data.DeleteMessage)
71+
{
72+
if (reaction.Message.IsSpecified)
73+
await reaction.Message.Value.DeleteAsync();
74+
}
75+
76+
if (_data.Cooldown.HasValue)
77+
if (_cooldowns.TryGetValue(Message.Id, out DateTime endsAt))
78+
{
79+
var difference = endsAt.Subtract(DateTime.UtcNow);
80+
if (difference.Ticks > 0)
81+
{
82+
return !_data.AllowMultipleTimes;
83+
}
84+
var time = DateTime.UtcNow.Add(_data.Cooldown.Value);
85+
_cooldowns.TryUpdate(Message.Id, time, endsAt);
86+
}
87+
else
88+
{
89+
_cooldowns.TryAdd(Message.Id, DateTime.UtcNow.Add(_data.Cooldown.Value));
90+
}
91+
92+
await reactionCallbackItem.Callback(Context);
93+
94+
return !_data.AllowMultipleTimes;
95+
}
96+
}
97+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Discord.Commands;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace Discord.Addons.Interactive
7+
{
8+
public class ReactionCallbackData
9+
{
10+
private readonly ICollection<ReactionCallbackItem> _items;
11+
12+
public string Text { get; }
13+
public Embed Embed { get; }
14+
public TimeSpan? Timeout { get; }
15+
public TimeSpan? Cooldown { get; }
16+
public IEnumerable<ReactionCallbackItem> Callbacks => _items;
17+
public bool RemoveReaction { get; }
18+
public bool DeleteMessage { get; }
19+
public bool AllowMultipleTimes { get; }
20+
21+
public ReactionCallbackData(string text, Embed embed = null, bool allowMultipleTimes = false, bool removeReaction = false, bool deleteMessage = false, TimeSpan? cooldown = null, TimeSpan ? timeout = null)
22+
{
23+
Text = text;
24+
Embed = embed;
25+
Timeout = timeout;
26+
RemoveReaction = removeReaction;
27+
DeleteMessage = deleteMessage;
28+
AllowMultipleTimes = allowMultipleTimes;
29+
Cooldown = cooldown;
30+
_items = new List<ReactionCallbackItem>();
31+
}
32+
33+
public ReactionCallbackData WithCallback(IEmote reaction, Func<SocketCommandContext, Task> callback)
34+
{
35+
var item = new ReactionCallbackItem(reaction, callback);
36+
_items.Add(item);
37+
return this;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)