Skip to content

Commit

Permalink
- Handle missing permissions (discord) on tryhandlecommand gracefully.
Browse files Browse the repository at this point in the history
- Add TradeResult.TrainerTooSlow before exiting if no trade was done during the wait time (BDSP).
  • Loading branch information
bdawg1989 committed Aug 12, 2024
1 parent c07c659 commit de23a39
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
75 changes: 51 additions & 24 deletions SysBot.Pokemon.Discord/SysCord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Discord;
using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using PKHeX.Core;
Expand Down Expand Up @@ -483,37 +484,63 @@ private async Task TryHandleAttachmentAsync(SocketMessage msg)

private async Task<bool> TryHandleCommandAsync(SocketUserMessage msg, SocketCommandContext context, int pos)
{
var AbuseSettings = Hub.Config.TradeAbuse;

// Check if the user is in the bannedIDs list
if (msg.Author is SocketGuildUser user && AbuseSettings.BannedIDs.List.Any(z => z.ID == user.Id))
try
{
await msg.Channel.SendMessageAsync("You are banned from using this bot.").ConfigureAwait(false);
var AbuseSettings = Hub.Config.TradeAbuse;
// Check if the user is in the bannedIDs list
if (msg.Author is SocketGuildUser user && AbuseSettings.BannedIDs.List.Any(z => z.ID == user.Id))
{
await SysCord<T>.SafeSendMessageAsync(msg.Channel, "You are banned from using this bot.").ConfigureAwait(false);
return true;
}

var mgr = Manager;
if (!mgr.CanUseCommandUser(msg.Author.Id))
{
await SysCord<T>.SafeSendMessageAsync(msg.Channel, "You are not permitted to use this command.").ConfigureAwait(false);
return true;
}

if (!mgr.CanUseCommandChannel(msg.Channel.Id) && msg.Author.Id != mgr.Owner)
{
if (Hub.Config.Discord.ReplyCannotUseCommandInChannel)
await SysCord<T>.SafeSendMessageAsync(msg.Channel, "You can't use that command here.").ConfigureAwait(false);
return true;
}

var guild = msg.Channel is SocketGuildChannel g ? g.Guild.Name : "Unknown Guild";
await Log(new LogMessage(LogSeverity.Info, "Command", $"Executing command from {guild}#{msg.Channel.Name}:@{msg.Author.Username}. Content: {msg}")).ConfigureAwait(false);

var result = await _commands.ExecuteAsync(context, pos, _services).ConfigureAwait(false);

if (result.Error == CommandError.UnknownCommand)
return false;

if (!result.IsSuccess)
await SysCord<T>.SafeSendMessageAsync(msg.Channel, result.ErrorReason).ConfigureAwait(false);

return true;
}
catch (Exception ex)
{
await Log(new LogMessage(LogSeverity.Error, "Command", $"Error executing command: {ex.Message}", ex)).ConfigureAwait(false);
return false;
}
}

var mgr = Manager;
if (!mgr.CanUseCommandUser(msg.Author.Id))
private static async Task SafeSendMessageAsync(IMessageChannel channel, string message)
{
try
{
await msg.Channel.SendMessageAsync("You are not permitted to use this command.").ConfigureAwait(false);
return true;
await channel.SendMessageAsync(message).ConfigureAwait(false);
}
if (!mgr.CanUseCommandChannel(msg.Channel.Id) && msg.Author.Id != mgr.Owner)
catch (HttpException ex) when (ex.DiscordCode == DiscordErrorCode.InsufficientPermissions) // Missing Permissions
{
if (Hub.Config.Discord.ReplyCannotUseCommandInChannel)
await msg.Channel.SendMessageAsync("You can't use that command here.").ConfigureAwait(false);
return true;
await Log(new LogMessage(LogSeverity.Warning, "Command", $"Missing permissions to send message in channel {channel.Name}")).ConfigureAwait(false);
}
catch (Exception ex)
{
await Log(new LogMessage(LogSeverity.Error, "Command", $"Error sending message: {ex.Message}", ex)).ConfigureAwait(false);
}

var guild = msg.Channel is SocketGuildChannel g ? g.Guild.Name : "Unknown Guild";
await Log(new LogMessage(LogSeverity.Info, "Command", $"Executing command from {guild}#{msg.Channel.Name}:@{msg.Author.Username}. Content: {msg}")).ConfigureAwait(false);
var result = await _commands.ExecuteAsync(context, pos, _services).ConfigureAwait(false);

if (result.Error == CommandError.UnknownCommand)
return false;

if (!result.IsSuccess)
await msg.Channel.SendMessageAsync(result.ErrorReason).ConfigureAwait(false);
return true;
}
}
14 changes: 13 additions & 1 deletion SysBot.Pokemon/BDSP/BotTrade/PokeTradeBotBS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,19 @@ private async Task<PokeTradeResult> PerformLinkCodeTrade(SAV8BS sav, PokeTradeDe
// Wait for user input... Needs to be different from the previously offered Pokémon.
var tradeOffered = await ReadUntilChanged(LinkTradePokemonOffset, lastOffered, 25_000, 1_000, false, true, token).ConfigureAwait(false);
if (!tradeOffered)
return PokeTradeResult.TrainerTooSlow;
{
// Check if b1s1 slot didn't change at all
var currentB1S1 = await ReadPokemon(BoxStartOffset, BoxFormatSlotSize, token).ConfigureAwait(false);
if (SearchUtil.HashByDetails(currentB1S1) == SearchUtil.HashByDetails(toSend) && currentB1S1.Checksum == toSend.Checksum)
{
Log("No Pokémon change detected in b1s1. Trainer too slow.");

// Exit back to overworld
await EnsureOutsideOfUnionRoom(token).ConfigureAwait(false);

return PokeTradeResult.TrainerTooSlow;
}
}

// If we detected a change, they offered something.
var offered = await ReadPokemon(LinkTradePokemonOffset, BoxFormatSlotSize, token).ConfigureAwait(false);
Expand Down

0 comments on commit de23a39

Please sign in to comment.