Skip to content

Commit

Permalink
added user commands and pokemon/guild filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ExtraConcentratedJuice committed Jun 28, 2018
1 parent 944fea8 commit 161bdc5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
using System.Collections.Generic;
using System.Text;

namespace PokecordCatcherBot
namespace PokecordCatcherBot.Models
{
public class Configuration
{
public string Token { get; set; }
public string PokecordPrefix { get; set; }
public string UserbotPrefix { get; set; }
public ulong OwnerID { get; set; }
public bool EnableLogging { get; set; }
public bool EnableCatchResponse { get; set; }
public string CatchResponse { get; set; }

public string[] WhitelistedPokemon { get; set; }
public ulong[] WhitelistedGuilds { get; set; }
}
}
12 changes: 12 additions & 0 deletions PokecordCatcher/Models/State.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace PokecordCatcherBot.Models
{
public class State
{
public bool WhitelistGuilds { get; set; }
public bool WhitelistPokemon { get; set; }
}
}
72 changes: 69 additions & 3 deletions PokecordCatcher/PokecordCatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
using Shipwreck.Phash;
using Shipwreck.Phash.Bitmaps;
using Discord.Rest;
using PokecordCatcherBot.Models;

namespace PokecordCatcherBot
{
public class PokecordCatcher
{
public const ulong POKECORD_ID = 365975655608745985;

public Configuration Configuration { get; }
public Configuration Configuration { get; private set; }
public State State { get; private set; }
public DiscordSocketClient Client { get; }

private readonly HttpClient http = new HttpClient();
Expand All @@ -32,6 +34,18 @@ public PokecordCatcher(Dictionary<string, byte[]> pokemonHashes)

Configuration = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("config.json"));

if (File.Exists("state.data"))
State = JsonConvert.DeserializeObject<State>(File.ReadAllText("state.data"));
else
{
State = new State
{
WhitelistGuilds = false,
WhitelistPokemon = false
};
File.WriteAllText("state.data", JsonConvert.SerializeObject(State));
}

Client = new DiscordSocketClient(new DiscordSocketConfig
{
#if DEBUG
Expand All @@ -55,6 +69,51 @@ public PokecordCatcher(Dictionary<string, byte[]> pokemonHashes)

private async Task OnMessage(SocketMessage msg)
{
if (msg.Content.StartsWith(Configuration.UserbotPrefix) && msg.Author.Id == Configuration.OwnerID)
{
var args = msg.Content.Split(' ').ToList();
var command = args[0].Substring(Configuration.UserbotPrefix.Length);
args.RemoveAt(0);

if (command == "status")
{
var props = typeof(State).GetProperties();
var propData = new Dictionary<string, object>();

foreach (var prop in props)
propData[prop.Name] = prop.GetValue(State);

await msg.Channel.SendMessageAsync($"```{String.Join('\n', propData.Select(x => $"{x.Key}: {x.Value}"))}```");
}

if (command == "reload")
{
Configuration = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("config.json"));
await msg.Channel.SendMessageAsync("Configuration reloaded.");
}

if (command == "toggleguilds")
{
State.WhitelistGuilds = !State.WhitelistGuilds;
await msg.Channel.SendMessageAsync("Whitelisting of guilds has been toggled to " + State.WhitelistGuilds);
File.WriteAllText("state.data", JsonConvert.SerializeObject(State));
}

if (command == "togglepokemon")
{
State.WhitelistPokemon = !State.WhitelistPokemon;
await msg.Channel.SendMessageAsync("Whitelisting of pokemon has been toggled to " + State.WhitelistPokemon);
File.WriteAllText("state.data", JsonConvert.SerializeObject(State));
}

return;
}

var guild = ((SocketGuildChannel)msg.Channel).Guild;

if (State.WhitelistGuilds && !Configuration.WhitelistedGuilds.Contains(guild.Id))
return;

if (msg.Author.Id != POKECORD_ID || msg.Embeds?.Count == 0)
return;

Expand All @@ -73,6 +132,13 @@ private async Task OnMessage(SocketMessage msg)

Console.WriteLine($"Found pokemon in {watch.ElapsedMilliseconds}ms");

if (State.WhitelistPokemon && !Configuration.WhitelistedPokemon.Any(x => x.Equals(name, StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("Pokemon is not whitelisted, ignoring.");
Logger.Log($"Ignored a {name} in #{msg.Channel.Name} ({guild.Name})");
return;
}

var resp = await responseGrabber.SendMessageAndGrabResponse(
(ITextChannel)msg.Channel,
$"{Configuration.PokecordPrefix}catch {name}",
Expand All @@ -87,11 +153,11 @@ private async Task OnMessage(SocketMessage msg)
if (Configuration.EnableCatchResponse)
await msg.Channel.SendMessageAsync(Configuration.CatchResponse);

Logger.Log($"Caught a {name} in #{resp.Channel.Name} ({((SocketGuildChannel)resp.Channel).Guild.Name})");
Logger.Log($"Caught a {name} in #{resp.Channel.Name} ({guild.Name})");
}
else
{
Logger.Log($"Failed to catch {name} in #{resp.Channel.Name} ({((SocketGuildChannel)resp.Channel).Guild.Name})");
Logger.Log($"Failed to catch {name} in #{resp.Channel.Name} ({guild.Name})");
}

Console.WriteLine();
Expand Down

0 comments on commit 161bdc5

Please sign in to comment.