Urban Dictionary command, small improvents in message handler, emojis are in embeds now, avatar command

This commit is contained in:
Runebaas 2017-11-11 16:20:26 +01:00
parent 667d928ca4
commit 45f289d071
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
7 changed files with 168 additions and 27 deletions

View file

@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
namespace Geekbot.net.Commands
{
public class AvatarGetter : ModuleBase
{
private readonly IErrorHandler _errorHandler;
public AvatarGetter(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
}
[Command("avatar", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Randomness)]
[Summary("Get someones avatar")]
public async Task getAvatar([Remainder, Summary("user")] IUser user = null)
{
try
{
if (user == null)
{
user = Context.User;
}
await ReplyAsync(user.GetAvatarUrl());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
}

View file

@ -46,10 +46,10 @@ namespace Geekbot.net.Commands
_errorHandler.HandleCommandException(e, Context); _errorHandler.HandleCommandException(e, Context);
} }
} }
}
private class CatResponse
public class CatResponse {
{ public string file { get; set; }
public string file { get; set; } }
} }
} }

View file

@ -46,10 +46,10 @@ namespace Geekbot.net.Commands
_errorHandler.HandleCommandException(e, Context); _errorHandler.HandleCommandException(e, Context);
} }
} }
}
private class DogResponse
public class DogResponse {
{ public string url { get; set; }
public string url { get; set; } }
} }
} }

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib; using Geekbot.net.Lib;
@ -30,9 +31,16 @@ namespace Geekbot.net.Commands
{ {
await ReplyAsync("I can't take that much at once!"); await ReplyAsync("I can't take that much at once!");
return; return;
} }
await ReplyAsync($"*{Context.User.Username}#{Context.User.Discriminator} said:*"); var eb = new EmbedBuilder();
await ReplyAsync(emojis); eb.WithAuthor(new EmbedAuthorBuilder()
{
IconUrl = Context.User.GetAvatarUrl(),
Name = $"{Context.User.Username}#{Context.User.Discriminator}"
});
eb.WithColor(new Color(59, 136, 195));
eb.Description = emojis;
await ReplyAsync("", false, eb.Build());
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Newtonsoft.Json;
namespace Geekbot.net.Commands
{
public class UrbanDictionary : ModuleBase
{
private readonly IErrorHandler _errorHandler;
public UrbanDictionary(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
}
[Command("urban", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Randomness)]
[Summary("Lookup something on urban dictionary")]
public async Task urbanDefine([Remainder, Summary("word")] string word)
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.urbandictionary.com");
var response = await client.GetAsync($"/v0/define?term={word}");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var definitions = JsonConvert.DeserializeObject<UrbanResponse>(stringResponse);
if (definitions.list.Count == 0)
{
await ReplyAsync("That word is not defined...");
return;
}
var definition = definitions.list.OrderBy(e => e.thumbs_up).First();
var eb = new EmbedBuilder();
eb.Title = definition.word;
eb.WithColor(new Color(239,255,0));
eb.Description = definition.definition;
eb.AddField("Example", definition.example);
eb.AddInlineField("Upvotes", definition.thumbs_up);
eb.AddInlineField("Downvotes", definition.thumbs_down);
eb.WithFooter(new EmbedFooterBuilder()
{
Text = definition.permalink
});
await ReplyAsync("", false, eb.Build());
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
private class UrbanResponse
{
public string[] tags { get; set; }
public string result_type { get; set; }
public List<UrbanListItem> list { get; set; }
}
private class UrbanListItem
{
public string definition { get; set; }
public string permalink { get; set; }
public string thumbs_up { get; set; }
public string author { get; set; }
public string word { get; set; }
public string defid { get; set; }
public string current_vote { get; set; }
public string example { get; set; }
public string thumbs_down { get; set; }
}
}
}

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
@ -37,8 +38,7 @@ namespace Geekbot.net
{ {
try try
{ {
var message = messageParam as SocketUserMessage; if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask;
if (message == null) return Task.CompletedTask;
if (message.Author.IsBot) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask;
var argPos = 0; var argPos = 0;
var lowCaseMsg = message.ToString().ToLower(); var lowCaseMsg = message.ToString().ToLower();
@ -65,18 +65,30 @@ namespace Geekbot.net
} }
} }
public Task UpdateStats(SocketMessage messsageParam) public Task UpdateStats(SocketMessage message)
{ {
var message = messsageParam; try
if (message == null) return Task.CompletedTask; {
if (message == null) return Task.CompletedTask;
var channel = (SocketGuildChannel) message.Channel; if (message.Channel.Name.StartsWith('@'))
{
_redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); _logger.Information(
_redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); $"[Message] DM-Channel - {message.Channel.Name} - {message.Content}");
return Task.CompletedTask;
}
var channel = (SocketGuildChannel) message.Channel;
if (message.Author.IsBot) return Task.CompletedTask; _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString());
_logger.Information($"[Message] {channel.Guild.Name} - {message.Channel} - {message.Author.Username} - {message.Content}"); _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString());
if (message.Author.IsBot) return Task.CompletedTask;
_logger.Information(
$"[Message] {channel.Guild.Name} ({channel.Guild.Id}) - {message.Channel} ({message.Channel.Id}) - {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) - {message.Content}");
}
catch (Exception e)
{
_logger.Error(e, "Could not process message stats");
}
return Task.CompletedTask; return Task.CompletedTask;
} }

View file

@ -64,7 +64,6 @@ namespace Geekbot.net
{ {
LogLevel = LogSeverity.Verbose, LogLevel = LogSeverity.Verbose,
MessageCacheSize = 1000 MessageCacheSize = 1000
// AlwaysDownloadUsers = true
}); });
client.Log += DiscordLogger; client.Log += DiscordLogger;
commands = new CommandService(); commands = new CommandService();
@ -149,7 +148,7 @@ namespace Geekbot.net
if (isConneted) if (isConneted)
{ {
await client.SetGameAsync(redis.StringGet("Game")); await client.SetGameAsync(redis.StringGet("Game"));
logger.Information($"[Geekbot] Now Connected to {client.Guilds.Count} Servers"); logger.Information($"[Geekbot] Now Connected as {client.CurrentUser.Username} to {client.Guilds.Count} Servers");
logger.Information("[Geekbot] Registering Stuff"); logger.Information("[Geekbot] Registering Stuff");