geekbot/Geekbot.net/Commands/Games/Overwatch.cs

131 lines
4.5 KiB
C#
Raw Normal View History

2017-10-13 23:40:39 +02:00
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.UserRepository;
2017-10-13 23:40:39 +02:00
using OverwatchAPI;
using OverwatchAPI.Config;
namespace Geekbot.net.Commands.Games
2017-10-13 23:40:39 +02:00
{
[Group("ow")]
public class Overwatch : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IUserRepository _userRepository;
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
public Overwatch(IErrorHandler errorHandler, IUserRepository userRepository)
2017-10-13 23:40:39 +02:00
{
_errorHandler = errorHandler;
_userRepository = userRepository;
}
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
[Remarks(CommandCategories.Games)]
2018-04-30 23:44:19 +02:00
public async Task OwProfile()
2017-10-13 23:40:39 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag");
2017-10-13 23:40:39 +02:00
if (string.IsNullOrEmpty(tag))
{
await ReplyAsync("You have no battle Tag saved, use `!battletag`");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var profile = await CreateProfile(tag);
2017-10-13 23:40:39 +02:00
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
2017-12-29 01:53:50 +01:00
await ReplyAsync("", false, profile.Build());
2017-10-13 23:40:39 +02:00
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
2017-12-29 01:53:50 +01:00
2017-10-13 23:40:39 +02:00
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
[Remarks(CommandCategories.Games)]
2018-04-30 23:44:19 +02:00
public async Task OwProfile([Summary("BattleTag")] string tag)
2017-10-13 23:40:39 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
if (!BattleTag.IsValidTag(tag))
2017-10-13 23:40:39 +02:00
{
await ReplyAsync("That doesn't seem to be a valid battletag...");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var profile = await CreateProfile(tag);
2017-10-13 23:40:39 +02:00
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
2017-12-29 01:53:50 +01:00
2017-10-13 23:40:39 +02:00
await ReplyAsync("", false, profile.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only.")]
[Remarks(CommandCategories.Games)]
2018-04-30 23:44:19 +02:00
public async Task OwProfile([Summary("@someone")] IUser user)
2017-10-13 23:40:39 +02:00
{
try
{
2018-04-30 23:44:19 +02:00
var tag = _userRepository.GetUserSetting(user.Id, "BattleTag");
2017-10-13 23:40:39 +02:00
if (string.IsNullOrEmpty(tag))
{
await ReplyAsync("This user didn't set a battletag");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
var profile = await CreateProfile(tag);
2017-10-13 23:40:39 +02:00
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
2017-12-29 01:53:50 +01:00
2017-10-13 23:40:39 +02:00
await ReplyAsync("", false, profile.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
2018-04-30 23:44:19 +02:00
private async Task<EmbedBuilder> CreateProfile(string battletag)
2017-10-13 23:40:39 +02:00
{
2018-04-28 01:28:48 +02:00
var owConfig = new OverwatchConfig.Builder().WithPlatforms(Platform.Pc);
2017-10-13 23:40:39 +02:00
using (var owClient = new OverwatchClient(owConfig))
{
var player = await owClient.GetPlayerAsync(battletag);
2017-12-29 01:53:50 +01:00
if (player.Username == null) return null;
2017-10-13 23:40:39 +02:00
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(player.ProfilePortraitUrl)
.WithName(player.Username));
eb.Url = player.ProfileUrl;
eb.AddInlineField("Level", player.PlayerLevel);
2017-12-29 01:53:50 +01:00
eb.AddInlineField("Current Rank",
player.CompetitiveRank > 0 ? player.CompetitiveRank.ToString() : "Unranked");
2017-10-13 23:40:39 +02:00
return eb;
}
}
}
2017-12-29 01:53:50 +01:00
}