-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementar bot con comandos y algunos comentarios
- Loading branch information
Showing
16 changed files
with
589 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,53 @@ | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.WebSocket; | ||
using ClassLibrary; | ||
|
||
namespace Library.Commands; | ||
|
||
public class BattleCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'battle' del bot. Este comando une al | ||
/// jugador que envía el mensaje con el oponente que se recibe como parámetro, | ||
/// si lo hubiera, en una batalla; si no se recibe un oponente, lo une con | ||
/// cualquiera que esté esperando para jugar. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class BattleCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
/// <summary> | ||
/// Implementa el comando 'battle'. Este comando une al jugador que envía el | ||
/// mensaje a la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
[Command("battle")] | ||
[Summary( | ||
""" | ||
Une al jugador que envía el mensaje con el oponente que se recibe | ||
como parámetro, si lo hubiera, en una batalla; si no se recibe un | ||
oponente, lo une con cualquiera que esté esperando para jugar. | ||
""")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync( | ||
[Remainder] | ||
[Summary("Display name del oponente, opcional")] | ||
string? opponentDisplayName = null) | ||
{ | ||
string displayName = CommandHelper.GetDisplayName(Context); | ||
|
||
SocketGuildUser? opponentUser = CommandHelper.GetUser( | ||
Context, opponentDisplayName); | ||
|
||
string result; | ||
if (opponentUser != null) | ||
{ | ||
result = Facade.Instance.StartBattle(displayName, opponentUser.DisplayName); | ||
await Context.Message.Author.SendMessageAsync(result); | ||
await opponentUser.SendMessageAsync(result); | ||
} | ||
else | ||
{ | ||
result = $"No hay un usuario {opponentDisplayName}"; | ||
} | ||
|
||
await ReplyAsync(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,53 @@ | ||
using Discord.Commands; | ||
using Discord.WebSocket; | ||
|
||
namespace Library.Commands; | ||
|
||
public class CommandHelper | ||
public static class CommandHelper | ||
{ | ||
|
||
public static string GetDisplayName( | ||
SocketCommandContext context, | ||
string? name = null) | ||
{ | ||
if (name == null) | ||
{ | ||
name = context.Message.Author.Username; | ||
} | ||
|
||
foreach (SocketGuildUser user in context.Guild.Users) | ||
{ | ||
if (user.Username == name | ||
|| user.DisplayName == name | ||
|| user.Nickname == name | ||
|| user.GlobalName == name) | ||
{ | ||
return user.DisplayName; | ||
} | ||
} | ||
|
||
return name; | ||
} | ||
|
||
public static SocketGuildUser? GetUser( | ||
SocketCommandContext context, | ||
string? name) | ||
{ | ||
if (name == null) | ||
{ | ||
return null; | ||
} | ||
|
||
foreach (SocketGuildUser user in context.Guild.Users) | ||
{ | ||
if (user.Username == name | ||
|| user.DisplayName == name | ||
|| user.Nickname == name | ||
|| user.GlobalName == name) | ||
{ | ||
return user; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
using Discord.Commands; | ||
using ClassLibrary; | ||
|
||
namespace Library.Commands; | ||
|
||
public class JoinCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'join' del bot. Este comando une al jugador | ||
/// que envía el mensaje a la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class JoinCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
/// <summary> | ||
/// Implementa el comando 'join'. Este comando une al jugador que envía el | ||
/// mensaje a la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
[Command("join")] | ||
[Summary("Une el usuario que envía el mensaje a la lista de espera")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync() | ||
{ | ||
string displayName = CommandHelper.GetDisplayName(Context); | ||
string result = Facade.Instance.AddPlayerToWaitingList(displayName); | ||
await ReplyAsync(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
using Discord.Commands; | ||
using ClassLibrary; | ||
|
||
namespace Library.Commands; | ||
|
||
public class LeaveCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'leave' del bot. Este comando remueve el | ||
/// jugador que envía el mensaje de la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class LeaveCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
/// <summary> | ||
/// Implementa el comando 'leave' del bot. Este comando remueve el jugador | ||
/// que envía el mensaje de la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
[Command("leave")] | ||
[Summary("Remueve el usuario que envía el mensaje a la lista de espera")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync() | ||
{ | ||
string displayName = CommandHelper.GetDisplayName(Context); | ||
string result = Facade.Instance.RemovePlayerFromWaitingList(displayName); | ||
await ReplyAsync(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,89 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json.Nodes; | ||
using Microsoft.Extensions.Logging; | ||
using Discord.Commands; | ||
|
||
namespace Library.Commands; | ||
|
||
public class PokemonNameCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'name' del bot. Este comando retorna el | ||
/// nombre de un Pokémon dado su identificador. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class PokemonNameCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
} | ||
private readonly ILogger<PokemonNameCommand> logger; | ||
private readonly HttpClient httpClient; | ||
|
||
/// <summary> | ||
/// Inicializa una nueva instancia de la clase | ||
/// <see cref="PokemonNameCommand"/> con los valores recibidos como | ||
/// argumento. | ||
/// </summary> | ||
/// <param name="logger">El servicio de logging a utilizar.</param> | ||
public PokemonNameCommand(ILogger<PokemonNameCommand> logger) | ||
{ | ||
this.logger = logger; | ||
|
||
httpClient = new HttpClient(); | ||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
httpClient.DefaultRequestHeaders.Add("User-Agent", "DiscordBot"); | ||
} | ||
|
||
/// <summary> | ||
/// Implementa el comando 'name'. Este comando retorna el nombre de un | ||
/// Pokémon dado su identificador. | ||
/// </summary> | ||
/// <param name="id">El identificador del Pokémon a buscar.</param> | ||
[Command("name")] | ||
[Summary("Busca el nombre de un Pokémon por identificador usando la PokéAPI")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync([Remainder][Summary("ID")] int id = 0) | ||
{ | ||
if (id <= 0) | ||
{ | ||
await ReplyAsync("Uso: !name <id>"); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var response = await httpClient.GetStringAsync($"https://pokeapi.co/api/v2/pokemon/{id}"); | ||
|
||
if (string.IsNullOrEmpty(response)) | ||
{ | ||
await ReplyAsync($"No encontré nada para {id}"); | ||
return; | ||
} | ||
|
||
JsonNode? pokemonNode = JsonNode.Parse(response); | ||
JsonNode? nameNode = pokemonNode?["name"]; | ||
|
||
if (pokemonNode == null || nameNode == null) | ||
{ | ||
await ReplyAsync($"No encontré el nombre de {id}"); | ||
} | ||
else | ||
{ | ||
await ReplyAsync(nameNode.GetValue<string>()); | ||
} | ||
} | ||
catch (HttpRequestException exception) | ||
{ | ||
if (exception.StatusCode == HttpStatusCode.NotFound) | ||
{ | ||
await ReplyAsync("No lo encontré"); | ||
} | ||
else | ||
{ | ||
logger.LogError("HTTP error: {Message}", exception.Message); | ||
} | ||
|
||
} | ||
catch (Exception exception) | ||
{ | ||
logger.LogError("Exception: {Message}", exception.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,46 @@ | ||
using Discord.Commands; | ||
using Discord.WebSocket; | ||
using ClassLibrary; | ||
|
||
namespace Library.Commands; | ||
|
||
public class UserInfoCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'userinfo', alias 'who' o 'whois' del bot. | ||
/// Este comando retorna información sobre el usuario que envía el mensaje o sobre | ||
/// otro usuario si se incluye como parámetro.. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class UserInfoCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
/// <summary> | ||
/// Implementa el comando 'userinfo', alias 'who' o 'whois' del bot. | ||
/// </summary> | ||
/// <param name="displayName">El nombre de usuario de Discord a buscar.</param> | ||
[Command("who")] | ||
[Summary( | ||
""" | ||
Devuelve información sobre el usuario que se indica como parámetro o | ||
sobre el usuario que envía el mensaje si no se indica otro usuario. | ||
""")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync( | ||
[Remainder][Summary("El usuario del que tener información, opcional")] | ||
string? displayName = null) | ||
{ | ||
if (displayName != null) | ||
{ | ||
SocketGuildUser? user = CommandHelper.GetUser(Context, displayName); | ||
|
||
if (user == null) | ||
{ | ||
await ReplyAsync($"No puedo encontrar {displayName} en este servidor"); | ||
} | ||
} | ||
|
||
string userName = displayName ?? CommandHelper.GetDisplayName(Context); | ||
|
||
string result = Facade.Instance.PlayerIsWaiting(userName); | ||
|
||
await ReplyAsync(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
using Discord.Commands; | ||
using ClassLibrary; | ||
|
||
namespace Library.Commands; | ||
|
||
public class WaitingCommand | ||
/// <summary> | ||
/// Esta clase implementa el comando 'waitinglist' del bot. Este comando muestra | ||
/// la lista de jugadores esperando para jugar. | ||
/// </summary> | ||
// ReSharper disable once UnusedType.Global | ||
public class WaitingCommand : ModuleBase<SocketCommandContext> | ||
{ | ||
|
||
/// <summary> | ||
/// Implementa el comando 'waitinglist'. Este comando muestra la lista de | ||
/// jugadores esperando para jugar. | ||
/// </summary> | ||
[Command("waitinglist")] | ||
[Summary("Muestra los usuarios en la lista de espera")] | ||
// ReSharper disable once UnusedMember.Global | ||
public async Task ExecuteAsync() | ||
{ | ||
string result = Facade.Instance.GetAllPlayersWaiting(); | ||
|
||
await ReplyAsync(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.