Code improvements
This commit is contained in:
parent
d3662b9934
commit
ec5d403597
5 changed files with 53 additions and 14 deletions
|
@ -10,6 +10,9 @@
|
|||
<PackageReference Include="RestSharp.NetCore">
|
||||
<Version>105.2.4-rc4-24214-01</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="StackExchange.Redis">
|
||||
<Version>1.2.1</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Net.Http">
|
||||
<Version>4.3.1</Version>
|
||||
</PackageReference>
|
||||
|
|
24
Geekbot.net/Lib/CatClient.cs
Normal file
24
Geekbot.net/Lib/CatClient.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using RestSharp;
|
||||
|
||||
namespace Geekbot.net.Modules
|
||||
{
|
||||
public interface ICatClient
|
||||
{
|
||||
IRestClient Client { get; set; }
|
||||
}
|
||||
|
||||
public class CatClient : ICatClient
|
||||
{
|
||||
//Manage a restClient
|
||||
public CatClient()
|
||||
{
|
||||
Client = new RestClient("http://random.cat");
|
||||
}
|
||||
|
||||
public IRestClient Client { get; set; }
|
||||
}
|
||||
}
|
|
@ -7,20 +7,24 @@ namespace Geekbot.net.Lib
|
|||
public class StatsRecorder
|
||||
{
|
||||
|
||||
public static async Task Record(SocketMessage message)
|
||||
private SocketMessage message;
|
||||
|
||||
public StatsRecorder(SocketMessage message)
|
||||
{
|
||||
await UpdateUserRecordTask(message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
private static Task UpdateUserRecordTask(SocketMessage message)
|
||||
{
|
||||
return Task.Run(() => UpdateUserRecord(message));
|
||||
|
||||
}
|
||||
|
||||
private static void UpdateUserRecord(SocketMessage message)
|
||||
public async Task UpdateUserRecordAsync()
|
||||
{
|
||||
Console.WriteLine(message.Author.Username + " earned a point");
|
||||
await Task.FromResult(true);
|
||||
}
|
||||
|
||||
public async Task UpdateGuildRecordAsync()
|
||||
{
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
Console.WriteLine(channel.Guild.Name + " earned a point");
|
||||
await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,14 +7,18 @@ namespace Geekbot.net.Modules
|
|||
{
|
||||
public class Cat : ModuleBase
|
||||
{
|
||||
private readonly ICatClient catClient;
|
||||
public Cat(ICatClient catClient)
|
||||
{
|
||||
this.catClient = catClient;
|
||||
}
|
||||
|
||||
[Command("cat"), Summary("Return a random image of a cat.")]
|
||||
public async Task Say()
|
||||
{
|
||||
var client = new RestClient("http://random.cat");
|
||||
|
||||
var request = new RestRequest("meow.php", Method.GET);
|
||||
|
||||
var response = client.Execute<CatObject>(request);
|
||||
var response = catClient.Client.Execute<CatObject>(request);
|
||||
await ReplyAsync(response.Data.file);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ using Discord;
|
|||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Modules;
|
||||
|
||||
namespace Geekbot.net
|
||||
{
|
||||
|
@ -23,7 +24,7 @@ namespace Geekbot.net
|
|||
Console.WriteLine(" \\____|_____|_____|_|\\_\\____/ \\___/ |_|");
|
||||
Console.WriteLine("=========================================");
|
||||
Console.WriteLine("Starting...");
|
||||
new Program().MainAsync().GetAwaiter().GetResult();
|
||||
Task.WaitAll(new Program().MainAsync());
|
||||
}
|
||||
|
||||
public async Task MainAsync()
|
||||
|
@ -34,6 +35,7 @@ namespace Geekbot.net
|
|||
const string token = "MTgxMDkyOTgxMDUzNDU2Mzg0.C8_UTw.PvXLAVOTccbrWKLMeyvN9WqRPlU";
|
||||
|
||||
map = new DependencyMap();
|
||||
map.Add<ICatClient>(new CatClient());
|
||||
|
||||
await InstallCommands();
|
||||
Console.WriteLine("Connecting to Discord...");
|
||||
|
@ -75,7 +77,9 @@ namespace Geekbot.net
|
|||
|
||||
Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + message.Content);
|
||||
|
||||
await StatsRecorder.Record(message);
|
||||
var statsRecorder = new StatsRecorder(message);
|
||||
var updateUserRecordAsync = statsRecorder.UpdateUserRecordAsync();
|
||||
var updateGuildRecordAsync = statsRecorder.UpdateGuildRecordAsync();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue