Skip to content

Commit 4ad5950

Browse files
committed
merge
2 parents 7b6ed73 + 15e0a91 commit 4ad5950

File tree

5 files changed

+100
-41
lines changed

5 files changed

+100
-41
lines changed

SysBot.Pokemon.Discord/Commands/Bots/TradeModule.cs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Discord;
22
using Discord.Commands;
3+
using Discord.WebSocket;
34
using PKHeX.Core;
45
using System.Linq;
56
using System.Threading.Tasks;
@@ -34,22 +35,8 @@ public async Task GetTradeListAsync()
3435
[RequireQueueRole(nameof(DiscordManager.RolesTrade))]
3536
public async Task TradeAsyncAttach([Summary("Trade Code")] int code)
3637
{
37-
var attachment = Context.Message.Attachments.FirstOrDefault();
38-
if (attachment == default)
39-
{
40-
await ReplyAsync("No attachment provided!").ConfigureAwait(false);
41-
return;
42-
}
43-
44-
var att = await NetUtil.DownloadPKMAsync(attachment).ConfigureAwait(false);
45-
if (!att.Success || !(att.Data is PK8 pk8))
46-
{
47-
await ReplyAsync("No PK8 attachment provided!").ConfigureAwait(false);
48-
return;
49-
}
50-
5138
var sig = Context.User.GetFavor();
52-
await AddTradeToQueueAsync(code, Context.User.Username, pk8, sig).ConfigureAwait(false);
39+
await TradeAsyncAttach(code, sig, Context.User).ConfigureAwait(false);
5340
}
5441

5542
[Command("trade")]
@@ -85,7 +72,7 @@ public async Task TradeAsync([Summary("Trade Code")] int code, [Summary("Showdow
8572

8673
pkm.ResetPartyStats();
8774
var sig = Context.User.GetFavor();
88-
await AddTradeToQueueAsync(code, Context.User.Username, (PK8)pkm, sig).ConfigureAwait(false);
75+
await AddTradeToQueueAsync(code, Context.User.Username, (PK8)pkm, sig, Context.User).ConfigureAwait(false);
8976
}
9077

9178
[Command("trade")]
@@ -108,7 +95,59 @@ public async Task TradeAsyncAttach()
10895
await TradeAsyncAttach(code).ConfigureAwait(false);
10996
}
11097

111-
private async Task AddTradeToQueueAsync(int code, string trainerName, PK8 pk8, RequestSignificance sig)
98+
[Command("tradeUser")]
99+
[Alias("tu", "tradeOther")]
100+
[Summary("Makes the bot trade the mentioned user the attached file.")]
101+
[RequireSudo]
102+
public async Task TradeAsyncAttachUser([Summary("Trade Code")] int code, [Remainder]string _)
103+
{
104+
if (Context.Message.MentionedUsers.Count > 1)
105+
{
106+
await ReplyAsync("Too many mentions. Queue one user at a time.").ConfigureAwait(false);
107+
return;
108+
}
109+
110+
if (Context.Message.MentionedUsers.Count == 0)
111+
{
112+
await ReplyAsync("A user must be mentioned in order to do this.").ConfigureAwait(false);
113+
return;
114+
}
115+
116+
var usr = Context.Message.MentionedUsers.ElementAt(0);
117+
var sig = usr.GetFavor();
118+
await TradeAsyncAttach(code, sig, usr).ConfigureAwait(false);
119+
}
120+
121+
[Command("tradeUser")]
122+
[Alias("tu", "tradeOther")]
123+
[Summary("Makes the bot trade the mentioned user the attached file.")]
124+
[RequireSudo]
125+
public async Task TradeAsyncAttachUser([Remainder] string _)
126+
{
127+
var code = Info.GetRandomTradeCode();
128+
await TradeAsyncAttachUser(code, _).ConfigureAwait(false);
129+
}
130+
131+
private async Task TradeAsyncAttach(int code, RequestSignificance sig, SocketUser usr)
132+
{
133+
var attachment = Context.Message.Attachments.FirstOrDefault();
134+
if (attachment == default)
135+
{
136+
await ReplyAsync("No attachment provided!").ConfigureAwait(false);
137+
return;
138+
}
139+
140+
var att = await NetUtil.DownloadPKMAsync(attachment).ConfigureAwait(false);
141+
if (!att.Success || !(att.Data is PK8 pk8))
142+
{
143+
await ReplyAsync("No PK8 attachment provided!").ConfigureAwait(false);
144+
return;
145+
}
146+
147+
await AddTradeToQueueAsync(code, usr.Username, pk8, sig, usr).ConfigureAwait(false);
148+
}
149+
150+
private async Task AddTradeToQueueAsync(int code, string trainerName, PK8 pk8, RequestSignificance sig, SocketUser usr)
112151
{
113152
if (!pk8.CanBeTraded())
114153
{
@@ -123,7 +162,7 @@ private async Task AddTradeToQueueAsync(int code, string trainerName, PK8 pk8, R
123162
return;
124163
}
125164

126-
await Context.AddToQueueAsync(code, trainerName, sig, pk8, PokeRoutineType.LinkTrade, PokeTradeType.Specific).ConfigureAwait(false);
165+
await Context.AddToQueueAsync(code, trainerName, sig, pk8, PokeRoutineType.LinkTrade, PokeTradeType.Specific, usr).ConfigureAwait(false);
127166
}
128167
}
129168
}

SysBot.Pokemon.Discord/Helpers/DiscordTradeNotifier.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Discord;
22
using Discord.Commands;
3+
using Discord.WebSocket;
34
using PKHeX.Core;
45
using System;
56
using System.Linq;
@@ -11,50 +12,50 @@ namespace SysBot.Pokemon.Discord
1112
private T Data { get; }
1213
private PokeTradeTrainerInfo Info { get; }
1314
private int Code { get; }
14-
private SocketCommandContext Context { get; }
15+
private SocketUser Trader { get; }
1516
public Action<PokeRoutineExecutor>? OnFinish { private get; set; }
1617
public PokeTradeHub<PK8> Hub = SysCordInstance.Self.Hub;
1718

18-
public DiscordTradeNotifier(T data, PokeTradeTrainerInfo info, int code, SocketCommandContext context)
19+
public DiscordTradeNotifier(T data, PokeTradeTrainerInfo info, int code, SocketUser trader)
1920
{
2021
Data = data;
2122
Info = info;
2223
Code = code;
23-
Context = context;
24+
Trader = trader;
2425
}
2526

2627
public void TradeInitialize(PokeRoutineExecutor routine, PokeTradeDetail<T> info)
2728
{
2829
var receive = Data.Species == 0 ? string.Empty : $" ({Data.Nickname})";
29-
Context.User.SendMessageAsync($"Initializing trade{receive}. Please be ready. Your code is **{Code:0000 0000}**.").ConfigureAwait(false);
30+
Trader.SendMessageAsync($"Initializing trade{receive}. Please be ready. Your code is **{Code:0000 0000}**.").ConfigureAwait(false);
3031
}
3132

3233
public void TradeSearching(PokeRoutineExecutor routine, PokeTradeDetail<T> info)
3334
{
3435
var name = Info.TrainerName;
3536
var trainer = string.IsNullOrEmpty(name) ? string.Empty : $", {name}";
36-
Context.User.SendMessageAsync($"I'm waiting for you{trainer}! Your code is **{Code:0000 0000}**. My IGN is **{routine.InGameName}**.").ConfigureAwait(false);
37+
Trader.SendMessageAsync($"I'm waiting for you{trainer}! Your code is **{Code:0000 0000}**. My IGN is **{routine.InGameName}**.").ConfigureAwait(false);
3738
}
3839

3940
public void TradeCanceled(PokeRoutineExecutor routine, PokeTradeDetail<T> info, PokeTradeResult msg)
4041
{
4142
OnFinish?.Invoke(routine);
42-
Context.User.SendMessageAsync($"Trade canceled: {msg}").ConfigureAwait(false);
43+
Trader.SendMessageAsync($"Trade canceled: {msg}").ConfigureAwait(false);
4344
}
4445

4546
public void TradeFinished(PokeRoutineExecutor routine, PokeTradeDetail<T> info, T result)
4647
{
4748
OnFinish?.Invoke(routine);
4849
var tradedToUser = Data.Species;
4950
var message = tradedToUser != 0 ? $"Trade finished. Enjoy your {(Species)tradedToUser}!" : "Trade finished!";
50-
Context.User.SendMessageAsync(message).ConfigureAwait(false);
51+
Trader.SendMessageAsync(message).ConfigureAwait(false);
5152
if (result.Species != 0 && Hub.Config.Discord.ReturnPK8s)
52-
Context.User.SendPKMAsync(result, "Here's what you traded me!").ConfigureAwait(false);
53+
Trader.SendPKMAsync(result, "Here's what you traded me!").ConfigureAwait(false);
5354
}
5455

5556
public void SendNotification(PokeRoutineExecutor routine, PokeTradeDetail<T> info, string message)
5657
{
57-
Context.User.SendMessageAsync(message).ConfigureAwait(false);
58+
Trader.SendMessageAsync(message).ConfigureAwait(false);
5859
}
5960

6061
public void SendNotification(PokeRoutineExecutor routine, PokeTradeDetail<T> info, PokeTradeSummary message)
@@ -68,13 +69,13 @@ public void SendNotification(PokeRoutineExecutor routine, PokeTradeDetail<T> inf
6869
var msg = message.Summary;
6970
if (message.Details.Count > 0)
7071
msg += ", " + string.Join(", ", message.Details.Select(z => $"{z.Heading}: {z.Detail}"));
71-
Context.User.SendMessageAsync(msg).ConfigureAwait(false);
72+
Trader.SendMessageAsync(msg).ConfigureAwait(false);
7273
}
7374

7475
public void SendNotification(PokeRoutineExecutor routine, PokeTradeDetail<T> info, T result, string message)
7576
{
7677
if (result.Species != 0 && (Hub.Config.Discord.ReturnPK8s || info.Type == PokeTradeType.Dump))
77-
Context.User.SendPKMAsync(result, message).ConfigureAwait(false);
78+
Trader.SendPKMAsync(result, message).ConfigureAwait(false);
7879
}
7980

8081
private void SendNotificationZ3(SeedSearchResult r)
@@ -89,7 +90,7 @@ private void SendNotificationZ3(SeedSearchResult r)
8990
x.IsInline = false;
9091
});
9192
var msg = $"Here are the details for `{r.Seed:X16}`:";
92-
Context.User.SendMessageAsync(msg, embed: embed.Build()).ConfigureAwait(false);
93+
Trader.SendMessageAsync(msg, embed: embed.Build()).ConfigureAwait(false);
9394
}
9495
}
9596
}

SysBot.Pokemon.Discord/Helpers/QueueExtensions.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Discord;
22
using Discord.Commands;
3+
using Discord.WebSocket;
34
using Discord.Net;
45
using PKHeX.Core;
56
using System.Threading.Tasks;
@@ -10,7 +11,7 @@ public static class QueueExtensions
1011
{
1112
private const uint MaxTradeCode = 99999999;
1213

13-
public static async Task AddToQueueAsync(this SocketCommandContext Context, int code, string trainer, RequestSignificance sig, PK8 trade, PokeRoutineType routine, PokeTradeType type)
14+
public static async Task AddToQueueAsync(this SocketCommandContext Context, int code, string trainer, RequestSignificance sig, PK8 trade, PokeRoutineType routine, PokeTradeType type, SocketUser trader)
1415
{
1516
if ((uint)code > MaxTradeCode)
1617
{
@@ -22,22 +23,23 @@ public static async Task AddToQueueAsync(this SocketCommandContext Context, int
2223
try
2324
{
2425
const string helper = "I've added you to the queue! I'll message you here when your trade is starting.";
25-
test = await Context.User.SendMessageAsync(helper).ConfigureAwait(false);
26+
test = await trader.SendMessageAsync(helper).ConfigureAwait(false);
2627
}
2728
catch (HttpException ex)
2829
{
2930
await Context.Channel.SendMessageAsync($"{ex.HttpCode}: {ex.Reason}!").ConfigureAwait(false);
30-
await Context.Channel.SendMessageAsync("You must enable private messages in order to be queued!").ConfigureAwait(false);
31+
var noAccessMsg = Context.User == trader ? "You must enable private messages in order to be queued!" : "The mentioned user must enable private messages in order for them to be queued!";
32+
await Context.Channel.SendMessageAsync(noAccessMsg).ConfigureAwait(false);
3133
return;
3234
}
3335

3436
// Try adding
35-
var result = Context.AddToTradeQueue(trade, code, trainer, sig, routine, type, out var msg);
37+
var result = Context.AddToTradeQueue(trade, code, trainer, sig, routine, type, trader, out var msg);
3638

3739
// Notify in channel
3840
await Context.Channel.SendMessageAsync(msg).ConfigureAwait(false);
3941
// Notify in PM to mirror what is said in the channel.
40-
await Context.User.SendMessageAsync(msg).ConfigureAwait(false);
42+
await trader.SendMessageAsync(msg).ConfigureAwait(false);
4143

4244
// Clean Up
4345
if (result)
@@ -53,14 +55,19 @@ public static async Task AddToQueueAsync(this SocketCommandContext Context, int
5355
}
5456
}
5557

56-
private static bool AddToTradeQueue(this SocketCommandContext Context, PK8 pk8, int code, string trainerName, RequestSignificance sig, PokeRoutineType type, PokeTradeType t, out string msg)
58+
public static async Task AddToQueueAsync(this SocketCommandContext Context, int code, string trainer, RequestSignificance sig, PK8 trade, PokeRoutineType routine, PokeTradeType type)
59+
{
60+
await AddToQueueAsync(Context, code, trainer, sig, trade, routine, type, Context.User).ConfigureAwait(false);
61+
}
62+
63+
private static bool AddToTradeQueue(this SocketCommandContext Context, PK8 pk8, int code, string trainerName, RequestSignificance sig, PokeRoutineType type, PokeTradeType t, SocketUser trader, out string msg)
5764
{
58-
var user = Context.User;
65+
var user = trader;
5966
var userID = user.Id;
6067
var name = user.Username;
6168

6269
var trainer = new PokeTradeTrainerInfo(trainerName);
63-
var notifier = new DiscordTradeNotifier<PK8>(pk8, trainer, code, Context);
70+
var notifier = new DiscordTradeNotifier<PK8>(pk8, trainer, code, user);
6471
var detail = new PokeTradeDetail<PK8>(pk8, trainer, notifier, t, code: code, sig == RequestSignificance.Favored);
6572
var trade = new TradeEntry<PK8>(detail, userID, type, name);
6673

SysBot.Pokemon/Actions/PokeRoutineExecutor.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,17 @@ public async Task CloseGame(PokeTradeHubConfig config, CancellationToken token)
302302

303303
public async Task StartGame(PokeTradeHubConfig config, CancellationToken token)
304304
{
305-
// Open game and select profile.
305+
// Open game.
306306
await Click(A, 1_000 + config.Timings.ExtraTimeLoadProfile, token).ConfigureAwait(false);
307+
308+
// Menus here can go in the order: Update Prompt -> Profile -> DLC check -> Unable to use DLC.
309+
// The user can optionally turn on the setting if they know of a breaking system update incoming.
310+
if (config.Timings.AvoidSystemUpdate)
311+
{
312+
await Click(DUP, 0_600, token).ConfigureAwait(false);
313+
await Click(A, 1_000 + config.Timings.ExtraTimeLoadProfile, token).ConfigureAwait(false);
314+
}
315+
307316
await Click(A, 1_000 + config.Timings.ExtraTimeCheckDLC, token).ConfigureAwait(false);
308317
// If they have DLC on the system and can't use it, requires an UP + A to start the game.
309318
// Should be harmless otherwise since they'll be in loading screen.
@@ -313,7 +322,7 @@ public async Task StartGame(PokeTradeHubConfig config, CancellationToken token)
313322
Log("Restarting the game!");
314323

315324
// Switch Logo lag, skip cutscene, game load screen
316-
await Task.Delay(13_000 + config.Timings.ExtraTimeLoadGame, token).ConfigureAwait(false);
325+
await Task.Delay(10_000 + config.Timings.ExtraTimeLoadGame, token).ConfigureAwait(false);
317326

318327
while (!(await IsOnOverworld(config, token).ConfigureAwait(false) || await IsInBattle(token)))
319328
await Click(A, 2_000, token).ConfigureAwait(false);

SysBot.Pokemon/Settings/TimingSettings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class TimingSettings
1818
public int ExtraTimeCheckDLC { get; set; } = 0;
1919

2020
[Category(OpenGame), Description("Extra time in milliseconds to wait before clicking A in title screen.")]
21-
public int ExtraTimeLoadGame { get; set; } = 3000;
21+
public int ExtraTimeLoadGame { get; set; } = 5000;
2222

2323
// Closing the game.
2424
[Category(CloseGame), Description("Extra time in milliseconds to wait after pressing HOME to minimize the game.")]
@@ -50,5 +50,8 @@ public class TimingSettings
5050

5151
[Category(Misc), Description("Time to wait after each keypress when navigating Switch menus or entering Link Code.")]
5252
public int KeypressTime { get; set; } = 200;
53+
54+
[Category(Misc), Description("Enable this to decline incoming system updates.")]
55+
public bool AvoidSystemUpdate { get; set; } = false;
5356
}
5457
}

0 commit comments

Comments
 (0)