!changelog command, slightly improved info command, foundation for voice features
This commit is contained in:
parent
12919acf95
commit
b45370cf9e
5 changed files with 219 additions and 4 deletions
91
Geekbot.net/Commands/Changelog.cs
Normal file
91
Geekbot.net/Commands/Changelog.cs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Discord.WebSocket;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Commands
|
||||||
|
{
|
||||||
|
public class Changelog : ModuleBase
|
||||||
|
{
|
||||||
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
private readonly DiscordSocketClient _client;
|
||||||
|
|
||||||
|
public Changelog(IErrorHandler errorHandler, DiscordSocketClient client)
|
||||||
|
{
|
||||||
|
_errorHandler = errorHandler;
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("changelog", RunMode = RunMode.Async)]
|
||||||
|
[Alias("updates")]
|
||||||
|
[Remarks(CommandCategories.Helpers)]
|
||||||
|
[Summary("Show the latest 5 updates")]
|
||||||
|
public async Task getChangelog()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var client = new HttpClient())
|
||||||
|
{
|
||||||
|
client.BaseAddress = new Uri("https://api.github.com");
|
||||||
|
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "http://developer.github.com/v3/#user-agent-required");
|
||||||
|
var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits");
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||||
|
var commits = JsonConvert.DeserializeObject<List<Commit>>(stringResponse);
|
||||||
|
var eb = new EmbedBuilder();
|
||||||
|
eb.WithColor(new Color(143, 165, 102));
|
||||||
|
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||||
|
{
|
||||||
|
IconUrl = _client.CurrentUser.GetAvatarUrl(),
|
||||||
|
Name = "Latest Updates",
|
||||||
|
Url = "https://geekbot.pizzaandcoffee.rocks/updates"
|
||||||
|
});
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach (var commit in commits.Take(10))
|
||||||
|
{
|
||||||
|
sb.AppendLine($"- {commit.commit.message} ({commit.commit.author.date:yyyy-MM-dd})");
|
||||||
|
}
|
||||||
|
eb.Description = sb.ToString();
|
||||||
|
eb.WithFooter(new EmbedFooterBuilder()
|
||||||
|
{
|
||||||
|
Text = $"List generated from github commits on {DateTime.Now:yyyy-MM-dd}"
|
||||||
|
});
|
||||||
|
await ReplyAsync("", false, eb.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Commit
|
||||||
|
{
|
||||||
|
public string sha { get; set; }
|
||||||
|
public CommitInfo commit { get; set; }
|
||||||
|
public Uri html_url { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CommitInfo
|
||||||
|
{
|
||||||
|
public commitAuthor author { get; set; }
|
||||||
|
public string message { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class commitAuthor
|
||||||
|
{
|
||||||
|
public string name { get; set; }
|
||||||
|
public string email { get; set; }
|
||||||
|
public DateTimeOffset date { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
@ -14,12 +15,14 @@ namespace Geekbot.net.Commands
|
||||||
private readonly IDatabase _redis;
|
private readonly IDatabase _redis;
|
||||||
private readonly IErrorHandler _errorHandler;
|
private readonly IErrorHandler _errorHandler;
|
||||||
private readonly DiscordSocketClient _client;
|
private readonly DiscordSocketClient _client;
|
||||||
|
private readonly CommandService _commands;
|
||||||
|
|
||||||
public Info(IDatabase redis, IErrorHandler errorHandler, DiscordSocketClient client)
|
public Info(IDatabase redis, IErrorHandler errorHandler, DiscordSocketClient client, CommandService commands)
|
||||||
{
|
{
|
||||||
_redis = redis;
|
_redis = redis;
|
||||||
_errorHandler = errorHandler;
|
_errorHandler = errorHandler;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
_commands = commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("info", RunMode = RunMode.Async)]
|
[Command("info", RunMode = RunMode.Async)]
|
||||||
|
@ -38,10 +41,13 @@ namespace Geekbot.net.Commands
|
||||||
var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime));
|
var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime));
|
||||||
|
|
||||||
eb.AddInlineField("Bot Name", _client.CurrentUser.Username);
|
eb.AddInlineField("Bot Name", _client.CurrentUser.Username);
|
||||||
eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count);
|
|
||||||
eb.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S");
|
|
||||||
eb.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}");
|
eb.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}");
|
||||||
eb.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/");
|
eb.AddInlineField("Library", "Discord.NET V1.0.2");
|
||||||
|
eb.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S");
|
||||||
|
eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count);
|
||||||
|
eb.AddInlineField("Total Commands", _commands.Commands.Count());
|
||||||
|
|
||||||
|
eb.AddField("Website", "https://geekbot.pizzaandcoffee.rocks/");
|
||||||
|
|
||||||
await ReplyAsync("", false, eb.Build());
|
await ReplyAsync("", false, eb.Build());
|
||||||
}
|
}
|
||||||
|
|
85
Geekbot.net/Commands/Voice.cs
Normal file
85
Geekbot.net/Commands/Voice.cs
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Audio;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Commands
|
||||||
|
{
|
||||||
|
public class Voice : ModuleBase
|
||||||
|
{
|
||||||
|
private readonly IErrorHandler _errorHandler;
|
||||||
|
private readonly IAudioUtils _audioUtils;
|
||||||
|
|
||||||
|
public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils)
|
||||||
|
{
|
||||||
|
_errorHandler = errorHandler;
|
||||||
|
_audioUtils = audioUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("join")]
|
||||||
|
public async Task JoinChannel()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Get the audio channel
|
||||||
|
var channel = (Context.User as IGuildUser)?.VoiceChannel;
|
||||||
|
if (channel == null)
|
||||||
|
{
|
||||||
|
await Context.Channel.SendMessageAsync(
|
||||||
|
"User must be in a voice channel, or a voice channel must be passed as an argument.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For the next step with transmitting audio, you would want to pass this Audio Client in to a service.
|
||||||
|
var audioClient = await channel.ConnectAsync();
|
||||||
|
_audioUtils.StoreAudioClient(Context.Guild.Id, audioClient);
|
||||||
|
await ReplyAsync($"Connected to {channel.Name}");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("disconnect")]
|
||||||
|
public async Task DisconnectChannel()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id);
|
||||||
|
if (audioClient == null)
|
||||||
|
{
|
||||||
|
await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await audioClient.StopAsync();
|
||||||
|
await ReplyAsync("Disconnected from channel!");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_errorHandler.HandleCommandException(e, Context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [Command("play")]
|
||||||
|
// public async Task play(IVoiceChannel channel = null)
|
||||||
|
// {
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id);
|
||||||
|
// if (audioClient == null)
|
||||||
|
// {
|
||||||
|
// await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// catch (Exception e)
|
||||||
|
// {
|
||||||
|
// _errorHandler.HandleCommandException(e, Context);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
31
Geekbot.net/Lib/AudioClientCache.cs
Normal file
31
Geekbot.net/Lib/AudioClientCache.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Discord.Audio;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Lib
|
||||||
|
{
|
||||||
|
public class AudioUtils : IAudioUtils
|
||||||
|
{
|
||||||
|
private Dictionary<ulong, IAudioClient> _audioClients;
|
||||||
|
|
||||||
|
public AudioUtils()
|
||||||
|
{
|
||||||
|
_audioClients = new Dictionary<ulong, IAudioClient>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IAudioClient GetAudioClient(ulong guildId)
|
||||||
|
{
|
||||||
|
return _audioClients[guildId];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StoreAudioClient(ulong guildId, IAudioClient client)
|
||||||
|
{
|
||||||
|
_audioClients[guildId] = client;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IAudioUtils
|
||||||
|
{
|
||||||
|
IAudioClient GetAudioClient(ulong guildId);
|
||||||
|
void StoreAudioClient(ulong guildId, IAudioClient client);
|
||||||
|
}
|
||||||
|
}
|
|
@ -119,6 +119,7 @@ namespace Geekbot.net
|
||||||
var malClient = new MalClient(redis, logger);
|
var malClient = new MalClient(redis, logger);
|
||||||
var levelCalc = new LevelCalc();
|
var levelCalc = new LevelCalc();
|
||||||
var emojiConverter = new EmojiConverter();
|
var emojiConverter = new EmojiConverter();
|
||||||
|
var audioUtils = new AudioUtils();
|
||||||
|
|
||||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton(redis);
|
services.AddSingleton(redis);
|
||||||
|
@ -126,6 +127,7 @@ namespace Geekbot.net
|
||||||
services.AddSingleton<IUserRepository>(userRepository);
|
services.AddSingleton<IUserRepository>(userRepository);
|
||||||
services.AddSingleton<ILevelCalc>(levelCalc);
|
services.AddSingleton<ILevelCalc>(levelCalc);
|
||||||
services.AddSingleton<IEmojiConverter>(emojiConverter);
|
services.AddSingleton<IEmojiConverter>(emojiConverter);
|
||||||
|
services.AddSingleton<IAudioUtils>(audioUtils);
|
||||||
services.AddSingleton(randomClient);
|
services.AddSingleton(randomClient);
|
||||||
services.AddSingleton<IFortunesProvider>(fortunes);
|
services.AddSingleton<IFortunesProvider>(fortunes);
|
||||||
services.AddSingleton<IMediaProvider>(mediaProvider);
|
services.AddSingleton<IMediaProvider>(mediaProvider);
|
||||||
|
|
Loading…
Reference in a new issue