Upgraded to dotnet core 2 and small arch restructure

This commit is contained in:
Runebaas 2017-09-14 22:11:19 +02:00
parent e97ebf86ef
commit 7d0b0c4634
20 changed files with 438 additions and 591 deletions

View file

@ -1,20 +0,0 @@
using System.Collections.Generic;
namespace Geekbot.net.Lib.Dtos
{
class FourChanDto
{
public class BoardList
{
public List<Board> Boards { get; set; }
}
public class Board
{
public string board { get; set; }
public string title { get; set; }
public string ws_board { get; set; }
public string meta_description { get; set; }
}
}
}

View file

@ -1,19 +0,0 @@
using RestSharp;
namespace Geekbot.net.Lib.IClients
{
public interface ICatClient
{
IRestClient Client { get; set; }
}
public class CatClient : ICatClient
{
public CatClient()
{
Client = new RestClient("http://random.cat");
}
public IRestClient Client { get; set; }
}
}

View file

@ -1,19 +0,0 @@
using RestSharp;
namespace Geekbot.net.Lib.IClients
{
public interface IDogClient
{
IRestClient Client { get; set; }
}
public class DogClient : IDogClient
{
public DogClient()
{
Client = new RestClient("http://random.dog");
}
public IRestClient Client { get; set; }
}
}

View file

@ -1,29 +0,0 @@
using System;
namespace Geekbot.net.Lib.IClients
{
public interface IRandomClient
{
Random Client { get; set; }
}
public sealed class RandomClient : IRandomClient
{
public RandomClient()
{
try
{
Client = new Random();
}
catch (Exception)
{
Console.WriteLine("Start Redis pls...");
Environment.Exit(1);
}
}
public Random Client { get; set; }
}
}

View file

@ -1,30 +0,0 @@
using System;
using StackExchange.Redis;
namespace Geekbot.net.Lib.IClients
{
public interface IRedisClient
{
IDatabase Client { get; set; }
}
public sealed class RedisClient : IRedisClient
{
public RedisClient()
{
try
{
var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
Client = redis.GetDatabase(6);
}
catch (Exception)
{
Console.WriteLine("Start Redis pls...");
Environment.Exit(1);
}
}
public IDatabase Client { get; set; }
}
}

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.WebSocket; using Discord.WebSocket;
using Geekbot.net.Lib.IClients;
using StackExchange.Redis; using StackExchange.Redis;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
@ -12,10 +11,10 @@ namespace Geekbot.net.Lib
private readonly SocketMessage message; private readonly SocketMessage message;
private readonly IDatabase redis; private readonly IDatabase redis;
public StatsRecorder(SocketMessage message, IRedisClient redisClient) public StatsRecorder(SocketMessage message, IDatabase redis)
{ {
this.message = message; this.message = message;
redis = redisClient.Client; this.redis = redis;
} }
public async Task UpdateUserRecordAsync() public async Task UpdateUserRecordAsync()

View file

@ -1,16 +1,16 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
[Group("admin")] [Group("admin")]
public class AdminCmd : ModuleBase public class AdminCmd : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public AdminCmd(IRedisClient redisClient) public AdminCmd(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[RequireUserPermission(Discord.GuildPermission.Administrator)] [RequireUserPermission(Discord.GuildPermission.Administrator)]
@ -18,7 +18,7 @@ namespace Geekbot.net.Modules
public async Task SetWelcomeMessage([Remainder, Summary("The message")] string welcomeMessage) public async Task SetWelcomeMessage([Remainder, Summary("The message")] string welcomeMessage)
{ {
var key = Context.Guild.Id + "-welcomeMsg"; var key = Context.Guild.Id + "-welcomeMsg";
redis.Client.StringSet(key, welcomeMessage); redis.StringSet(key, welcomeMessage);
var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention);
await ReplyAsync("Welcome message has been changed\r\nHere is an example of how it would look:\r\n" + await ReplyAsync("Welcome message has been changed\r\nHere is an example of how it would look:\r\n" +
formatedMessage); formatedMessage);
@ -27,14 +27,14 @@ namespace Geekbot.net.Modules
[Command("youtubekey", RunMode = RunMode.Async), Summary("Set the youtube api key")] [Command("youtubekey", RunMode = RunMode.Async), Summary("Set the youtube api key")]
public async Task SetYoutubeKey([Summary("API Key")] string key) public async Task SetYoutubeKey([Summary("API Key")] string key)
{ {
var botOwner = redis.Client.StringGet("botOwner"); var botOwner = redis.StringGet("botOwner");
if (!Context.User.Id.ToString().Equals(botOwner.ToString())) if (!Context.User.Id.ToString().Equals(botOwner.ToString()))
{ {
await ReplyAsync($"Sorry, only the botowner can do this ({botOwner}"); await ReplyAsync($"Sorry, only the botowner can do this ({botOwner}");
return; return;
} }
redis.Client.StringSet("youtubeKey", key); redis.StringSet("youtubeKey", key);
await ReplyAsync("Apikey has been set"); await ReplyAsync("Apikey has been set");
} }
} }

View file

@ -1,25 +1,27 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients;
using RestSharp; using RestSharp;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Cat : ModuleBase public class Cat : ModuleBase
{ {
private readonly ICatClient catClient;
public Cat(ICatClient catClient)
{
this.catClient = catClient;
}
[Command("cat", RunMode = RunMode.Async), Summary("Return a random image of a cat.")] [Command("cat", RunMode = RunMode.Async), Summary("Return a random image of a cat.")]
public async Task Say() public async Task Say()
{ {
var catClient = new RestClient("http://random.cat");
var request = new RestRequest("meow.php", Method.GET); var request = new RestRequest("meow.php", Method.GET);
dynamic response = catClient.Client.Execute<dynamic>(request); catClient.ExecuteAsync<CatResponse>(request, async response => {
await ReplyAsync(response.Data["file"]); await ReplyAsync(response.Data.file);
});
} }
}
public class CatResponse
{
public string file { get; set; }
} }
} }

View file

@ -1,23 +1,22 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Choose : ModuleBase public class Choose : ModuleBase
{ {
private readonly IRandomClient rnd; private readonly Random rnd;
public Choose(IRandomClient randomClient) public Choose(Random RandomClient)
{ {
rnd = randomClient; rnd = RandomClient;
} }
[Command("choose", RunMode = RunMode.Async), Summary("Let the bot make a choice for you.")] [Command("choose", RunMode = RunMode.Async), Summary("Let the bot make a choice for you.")]
public async Task Command([Remainder, Summary("The choices, sepperated by a ;")] string choices) public async Task Command([Remainder, Summary("The choices, sepperated by a ;")] string choices)
{ {
var choicesArray = choices.Split(';'); var choicesArray = choices.Split(';');
var choice = rnd.Client.Next(choicesArray.Length); var choice = rnd.Next(choicesArray.Length);
await ReplyAsync($"I choose **{choicesArray[choice]}**"); await ReplyAsync($"I choose **{choicesArray[choice]}**");
} }
} }

View file

@ -2,16 +2,16 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Counters : ModuleBase public class Counters : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public Counters(IRedisClient redisClient) public Counters(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[Command("good", RunMode = RunMode.Async), Summary("Increase Someones Karma")] [Command("good", RunMode = RunMode.Async), Summary("Increase Someones Karma")]
@ -29,11 +29,11 @@ namespace Geekbot.net.Modules
else else
{ {
var key = Context.Guild.Id + "-" + user.Id + "-karma"; var key = Context.Guild.Id + "-" + user.Id + "-karma";
var badJokes = (int)redis.Client.StringGet(key); var badJokes = (int)redis.StringGet(key);
var newBadJokes = badJokes + 1; var newBadJokes = badJokes + 1;
redis.Client.StringSet(key, newBadJokes.ToString()); redis.StringSet(key, newBadJokes.ToString());
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout"; var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
redis.Client.StringSet(lastKey, GetNewLastKarma()); redis.StringSet(lastKey, GetNewLastKarma());
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder() eb.WithAuthor(new EmbedAuthorBuilder()
@ -64,11 +64,11 @@ namespace Geekbot.net.Modules
else else
{ {
var key = Context.Guild.Id + "-" + user.Id + "-karma"; var key = Context.Guild.Id + "-" + user.Id + "-karma";
var badJokes = (int)redis.Client.StringGet(key); var badJokes = (int)redis.StringGet(key);
var newBadJokes = badJokes - 1; var newBadJokes = badJokes - 1;
redis.Client.StringSet(key, newBadJokes.ToString()); redis.StringSet(key, newBadJokes.ToString());
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout"; var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
redis.Client.StringSet(lastKey, GetNewLastKarma()); redis.StringSet(lastKey, GetNewLastKarma());
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder() eb.WithAuthor(new EmbedAuthorBuilder()
@ -87,7 +87,7 @@ namespace Geekbot.net.Modules
private int GetLastKarma() private int GetLastKarma()
{ {
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout"; var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
var redisReturn = redis.Client.StringGet(lastKey); var redisReturn = redis.StringGet(lastKey);
if (!int.TryParse(redisReturn.ToString(), out var i)) if (!int.TryParse(redisReturn.ToString(), out var i))
{ {
i = GetUnixTimestamp(); i = GetUnixTimestamp();

View file

@ -1,25 +1,27 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients;
using RestSharp; using RestSharp;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Dog : ModuleBase public class Dog : ModuleBase
{ {
private readonly IDogClient dogClient;
public Dog(IDogClient dogClient)
{
this.dogClient = dogClient;
}
[Command("dog", RunMode = RunMode.Async), Summary("Return a random image of a dog.")] [Command("dog", RunMode = RunMode.Async), Summary("Return a random image of a dog.")]
public async Task Say() public async Task Say()
{ {
var dogClient = new RestClient("http://random.dog");
var request = new RestRequest("woof.json", Method.GET); var request = new RestRequest("woof.json", Method.GET);
Console.WriteLine(dogClient.BaseUrl);
dynamic response = dogClient.Client.Execute<dynamic>(request); dogClient.ExecuteAsync<DogResponse>(request, async response => {
await ReplyAsync(response.Data["url"]); await ReplyAsync(response.Data.url);
});
} }
} }
public class DogResponse
{
public string url { get; set; }
}
} }

View file

@ -2,16 +2,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class EightBall : ModuleBase public class EightBall : ModuleBase
{ {
private readonly IRandomClient rnd; private readonly Random rnd;
public EightBall(IRandomClient randomClient) public EightBall(Random RandomClient)
{ {
rnd = randomClient; rnd = RandomClient;
} }
[Command("8ball", RunMode = RunMode.Async), Summary("Ask 8Ball a Question.")] [Command("8ball", RunMode = RunMode.Async), Summary("Ask 8Ball a Question.")]
public async Task Ball([Remainder, Summary("The Question")] string echo) public async Task Ball([Remainder, Summary("The Question")] string echo)
@ -38,7 +37,7 @@ namespace Geekbot.net.Modules
"Outlook not so good", "Outlook not so good",
"Very doubtful"}; "Very doubtful"};
var answer = rnd.Client.Next(replies.Count); var answer = rnd.Next(replies.Count);
await ReplyAsync(replies[answer]); await ReplyAsync(replies[answer]);
} }
} }

View file

@ -1,43 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord.Commands;
using static Geekbot.net.Lib.Dtos.FourChanDto;
using Geekbot.net.Lib.IClients;
namespace Geekbot.net.Modules
{
public class FourChan : ModuleBase
{
[Command("4chan", RunMode = RunMode.Async), Summary("Get Something from 4chan")]
public async Task Chan([Summary("The someone")] string boardParam)
{
try
{
var boards = FourChanBoardClient.Boards();
var board = new Board();
foreach (var b in boards.getBoards())
{
if (b.board.Equals(boardParam))
{
board = b;
break;
}
}
if (board.board == boardParam)
{
await ReplyAsync($"{board.title} - {board.meta_description}");
} else
{
await ReplyAsync("Sorry, that board does not exist...");
}
}
catch (Exception e)
{
await ReplyAsync(e.Message);
}
}
}
}
// var boards = new List<string>["a", "b", "c", "d", "e", "f", "g", "gif", "h", "hr", "k", "m", "o", "p", "r", "s", "t", "u", "v", "vg", "vr", "w", "wg", "i", "ic", "r9k", "s4s", "cm", "hm", "lgbt", "y", "3", "aco", "adv", "an", "asp", "biz", "cgl", "ck", "co", "diy", "fa", "fit", "gd", "hc", "his", "int", "jp", "lit", "mlp", "mu", "n", "news", "out", "po", "pol", "qst", "sci", "soc" / sp / tg / toy / trv / tv / vp / wsg / wsr /];

View file

@ -4,16 +4,16 @@ using Discord.Commands;
using Discord; using Discord;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using System.Linq; using System.Linq;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class GuildInfo : ModuleBase public class GuildInfo : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public GuildInfo(IRedisClient redisClient) public GuildInfo(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[Command("serverstats", RunMode = RunMode.Async), Summary("Show some info about the bot.")] [Command("serverstats", RunMode = RunMode.Async), Summary("Show some info about the bot.")]
@ -28,7 +28,7 @@ namespace Geekbot.net.Modules
var created = Context.Guild.CreatedAt; var created = Context.Guild.CreatedAt;
var age = Math.Floor((DateTime.Now - created).TotalDays); var age = Math.Floor((DateTime.Now - created).TotalDays);
var messages = redis.Client.StringGet($"{Context.Guild.Id}-messages"); var messages = redis.StringGet($"{Context.Guild.Id}-messages");
var level = LevelCalc.GetLevelAtExperience((int)messages); var level = LevelCalc.GetLevelAtExperience((int)messages);
eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)");

View file

@ -1,16 +1,16 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Info : ModuleBase public class Info : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public Info(IRedisClient redisClient) public Info(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[Command("info", RunMode = RunMode.Async), Summary("Get Information about the bot")] [Command("info", RunMode = RunMode.Async), Summary("Get Information about the bot")]
@ -18,9 +18,9 @@ namespace Geekbot.net.Modules
{ {
var eb = new EmbedBuilder(); var eb = new EmbedBuilder();
eb.WithTitle("Geekbot V3"); eb.WithTitle("Geekbot V3.1");
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.Client.StringGet("botOwner"))).Result; var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.StringGet("botOwner"))).Result;
eb.AddInlineField("Status", Context.Client.ConnectionState.ToString()) eb.AddInlineField("Status", Context.Client.ConnectionState.ToString())
.AddInlineField("Bot Name", Context.Client.CurrentUser.Username) .AddInlineField("Bot Name", Context.Client.CurrentUser.Username)

View file

@ -1,24 +1,25 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Roll : ModuleBase public class Roll : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
private readonly IRandomClient rnd; private readonly Random rnd;
public Roll(IRedisClient redisClient, IRandomClient randomClient) public Roll(IDatabase redis, Random RandomClient)
{ {
redis = redisClient; this.redis = redis;
rnd = randomClient; this.rnd = RandomClient;
} }
[Command("roll", RunMode = RunMode.Async), Summary("Roll a number between 1 and 100.")] [Command("roll", RunMode = RunMode.Async), Summary("Roll a number between 1 and 100.")]
public async Task RollCommand([Remainder, Summary("stuff...")] string stuff = "nothing") public async Task RollCommand([Remainder, Summary("stuff...")] string stuff = "nothing")
{ {
var number = rnd.Client.Next(1, 100); var number = rnd.Next(1, 100);
var guess = 1000; var guess = 1000;
int.TryParse(stuff, out guess); int.TryParse(stuff, out guess);
if (guess <= 100 && guess > 0) if (guess <= 100 && guess > 0)
@ -28,8 +29,8 @@ namespace Geekbot.net.Modules
{ {
await ReplyAsync($"Congratulations {Context.User.Username}, your guess was correct!"); await ReplyAsync($"Congratulations {Context.User.Username}, your guess was correct!");
var key = $"{Context.Guild.Id}-{Context.User.Id}-correctRolls"; var key = $"{Context.Guild.Id}-{Context.User.Id}-correctRolls";
var messages = (int)redis.Client.StringGet(key); var messages = (int)redis.StringGet(key);
redis.Client.StringSet(key, (messages + 1).ToString()); redis.StringSet(key, (messages + 1).ToString());
} }
} }
else else
@ -41,7 +42,7 @@ namespace Geekbot.net.Modules
[Command("dice", RunMode = RunMode.Async), Summary("Roll a dice")] [Command("dice", RunMode = RunMode.Async), Summary("Roll a dice")]
public async Task DiceCommand([Summary("The highest number on the dice")] int max = 6) public async Task DiceCommand([Summary("The highest number on the dice")] int max = 6)
{ {
var number = rnd.Client.Next(1, max); var number = rnd.Next(1, max);
await ReplyAsync(Context.Message.Author.Mention + ", you rolled " + number); await ReplyAsync(Context.Message.Author.Mention + ", you rolled " + number);
} }
} }

View file

@ -2,19 +2,19 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Ship : ModuleBase public class Ship : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
private readonly IRandomClient rnd; private readonly Random rnd;
public Ship(IRedisClient redisClient, IRandomClient randomClient) public Ship(IDatabase redis, Random RandomClient)
{ {
redis = redisClient; this.redis = redis;
rnd = randomClient; this.rnd = RandomClient;
} }
[Command("Ship", RunMode = RunMode.Async), Summary("Ask the Shipping meter")] [Command("Ship", RunMode = RunMode.Async), Summary("Ask the Shipping meter")]
@ -33,12 +33,12 @@ namespace Geekbot.net.Modules
dbstring = $"{Context.Guild.Id}-{dbstring}"; dbstring = $"{Context.Guild.Id}-{dbstring}";
Console.WriteLine(dbstring); Console.WriteLine(dbstring);
var dbval = redis.Client.StringGet(dbstring); var dbval = redis.StringGet(dbstring);
var shippingRate = 0; var shippingRate = 0;
if (dbval.IsNullOrEmpty) if (dbval.IsNullOrEmpty)
{ {
shippingRate = rnd.Client.Next(1, 100); shippingRate = rnd.Next(1, 100);
redis.Client.StringSet(dbstring, shippingRate); redis.StringSet(dbstring, shippingRate);
} }
else else
{ {

View file

@ -6,16 +6,16 @@ using System.Linq;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Geekbot.net.Lib.IClients; using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class UserInfo : ModuleBase public class UserInfo : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public UserInfo(IRedisClient redisClient) public UserInfo(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[Alias("stats")] [Alias("stats")]
@ -27,11 +27,11 @@ namespace Geekbot.net.Modules
var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays); var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays);
var key = Context.Guild.Id + "-" + userInfo.Id; var key = Context.Guild.Id + "-" + userInfo.Id;
var messages = (int)redis.Client.StringGet(key + "-messages"); var messages = (int)redis.StringGet(key + "-messages");
var level = LevelCalc.GetLevelAtExperience(messages); var level = LevelCalc.GetLevelAtExperience(messages);
var guildKey = Context.Guild.Id.ToString(); var guildKey = Context.Guild.Id.ToString();
var guildMessages = (int)redis.Client.StringGet(guildKey + "-messages"); var guildMessages = (int)redis.StringGet(guildKey + "-messages");
var percent = Math.Round((double)(100 * messages) / guildMessages, 2); var percent = Math.Round((double)(100 * messages) / guildMessages, 2);
@ -47,13 +47,13 @@ namespace Geekbot.net.Modules
.AddInlineField("Messages Sent", messages) .AddInlineField("Messages Sent", messages)
.AddInlineField("Server Total", $"{percent}%"); .AddInlineField("Server Total", $"{percent}%");
var karma = redis.Client.StringGet(key + "-karma"); var karma = redis.StringGet(key + "-karma");
if (!karma.IsNullOrEmpty) if (!karma.IsNullOrEmpty)
{ {
eb.AddInlineField("Karma", karma); eb.AddInlineField("Karma", karma);
} }
var correctRolls = redis.Client.StringGet($"{Context.Guild.Id}-{userInfo.Id}-correctRolls"); var correctRolls = redis.StringGet($"{Context.Guild.Id}-{userInfo.Id}-correctRolls");
if (!correctRolls.IsNullOrEmpty) if (!correctRolls.IsNullOrEmpty)
{ {
eb.AddInlineField("Guessed Rolls", correctRolls); eb.AddInlineField("Guessed Rolls", correctRolls);
@ -68,13 +68,13 @@ namespace Geekbot.net.Modules
{ {
await ReplyAsync("this will take a moment..."); await ReplyAsync("this will take a moment...");
var guildKey = Context.Guild.Id.ToString(); var guildKey = Context.Guild.Id.ToString();
var guildMessages = (int)redis.Client.StringGet(guildKey + "-messages"); var guildMessages = (int)redis.StringGet(guildKey + "-messages");
var allGuildUsers = await Context.Guild.GetUsersAsync(); var allGuildUsers = await Context.Guild.GetUsersAsync();
var unsortedDict = new Dictionary<string, int>(); var unsortedDict = new Dictionary<string, int>();
foreach(var user in allGuildUsers) foreach(var user in allGuildUsers)
{ {
var key = Context.Guild.Id + "-" + user.Id; var key = Context.Guild.Id + "-" + user.Id;
var messages = (int)redis.Client.StringGet(key + "-messages"); var messages = (int)redis.StringGet(key + "-messages");
if(messages > 0) { if(messages > 0) {
unsortedDict.Add($"{user.Username}#{user.Discriminator}", messages); unsortedDict.Add($"{user.Username}#{user.Discriminator}", messages);
} }

View file

@ -1,24 +1,24 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands; using Discord.Commands;
using Geekbot.net.Lib.IClients;
using Google.Apis.Services; using Google.Apis.Services;
using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3;
using StackExchange.Redis;
namespace Geekbot.net.Modules namespace Geekbot.net.Modules
{ {
public class Youtube : ModuleBase public class Youtube : ModuleBase
{ {
private readonly IRedisClient redis; private readonly IDatabase redis;
public Youtube(IRedisClient redisClient) public Youtube(IDatabase redis)
{ {
redis = redisClient; this.redis = redis;
} }
[Command("yt", RunMode = RunMode.Async), Summary("Search for something on youtube.")] [Command("yt", RunMode = RunMode.Async), Summary("Search for something on youtube.")]
public async Task Yt([Remainder, Summary("A Song Title")] string searchQuery) public async Task Yt([Remainder, Summary("A Song Title")] string searchQuery)
{ {
var key = redis.Client.StringGet("youtubeKey"); var key = redis.StringGet("youtubeKey");
if (key.IsNullOrEmpty) if (key.IsNullOrEmpty)
{ {
await ReplyAsync("No youtube key set, please tell my senpai to set one"); await ReplyAsync("No youtube key set, please tell my senpai to set one");
@ -47,8 +47,8 @@ namespace Geekbot.net.Modules
catch (Exception e) catch (Exception e)
{ {
await ReplyAsync("Something went wrong... informing my senpai..."); await ReplyAsync("Something went wrong... informing my senpai...");
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.Client.StringGet("botOwner"))).Result; var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.StringGet("botOwner"))).Result;
var dm = await botOwner.CreateDMChannelAsync(); var dm = await botOwner.GetOrCreateDMChannelAsync();
await dm.SendMessageAsync($"Something went wrong while getting a video from youtube:\r\n```\r\n{e.Message}\r\n```"); await dm.SendMessageAsync($"Something went wrong while getting a video from youtube:\r\n```\r\n{e.Message}\r\n```");
} }
} }

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -10,8 +9,8 @@ using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using Geekbot.net.Lib.IClients;
using Geekbot.net.Modules; using Geekbot.net.Modules;
using RestSharp;
using StackExchange.Redis; using StackExchange.Redis;
namespace Geekbot.net namespace Geekbot.net
@ -20,9 +19,10 @@ namespace Geekbot.net
{ {
private CommandService commands; private CommandService commands;
private DiscordSocketClient client; private DiscordSocketClient client;
private IRedisClient redis; private IDatabase redis;
private RedisValue token; private RedisValue token;
private ServiceCollection services; private IServiceCollection services;
private IServiceProvider servicesProvider;
private static void Main(string[] args) private static void Main(string[] args)
{ {
@ -41,25 +41,34 @@ namespace Geekbot.net
{ {
client = new DiscordSocketClient(); client = new DiscordSocketClient();
commands = new CommandService(); commands = new CommandService();
redis = new RedisClient();
token = redis.Client.StringGet("discordToken"); try
{
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
redis = redisMultiplexer.GetDatabase(6);
}
catch (Exception)
{
Console.WriteLine("Start Redis pls...");
Environment.Exit(1);
}
token = redis.StringGet("discordToken");
if (token.IsNullOrEmpty) if (token.IsNullOrEmpty)
{ {
Console.Write("Your bot Token: "); Console.Write("Your bot Token: ");
var newToken = Console.ReadLine(); var newToken = Console.ReadLine();
redis.Client.StringSet("discordToken", newToken); redis.StringSet("discordToken", newToken);
token = newToken; token = newToken;
Console.Write("Bot Owner User ID: "); Console.Write("Bot Owner User ID: ");
var ownerId = Console.ReadLine(); var ownerId = Console.ReadLine();
redis.Client.StringSet("botOwner", ownerId); redis.StringSet("botOwner", ownerId);
} }
services = new ServiceCollection(); services = new ServiceCollection();
services.AddSingleton<ICatClient>(new CatClient()); var RandomClient = new Random();
services.AddSingleton<IDogClient>(new DogClient()); services.AddSingleton(RandomClient);
services.AddSingleton<IRandomClient>(new RandomClient());
services.AddSingleton(redis); services.AddSingleton(redis);
Console.WriteLine("Connecting to Discord..."); Console.WriteLine("Connecting to Discord...");
@ -87,6 +96,7 @@ namespace Geekbot.net
client.MessageReceived += HandleMessageReceived; client.MessageReceived += HandleMessageReceived;
client.UserJoined += HandleUserJoined; client.UserJoined += HandleUserJoined;
await commands.AddModulesAsync(Assembly.GetEntryAssembly()); await commands.AddModulesAsync(Assembly.GetEntryAssembly());
servicesProvider = services.BuildServiceProvider();
Console.WriteLine("Done and ready for use...\n"); Console.WriteLine("Done and ready for use...\n");
} }
@ -124,14 +134,9 @@ namespace Geekbot.net
await message.Channel.SendMessageAsync("hui!!!"); await message.Channel.SendMessageAsync("hui!!!");
return; return;
} }
// if (message.ToString().ToLower().Contains("teamspeak") || message.ToString().ToLower().Contains("skype"))
// {
// await message.Channel.SendMessageAsync("How dare you to use such a filthy word in here http://bit.ly/2poL2IZ");
// return;
// }
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
var context = new CommandContext(client, message); var context = new CommandContext(client, message);
Task.Run(async () => await commands.ExecuteAsync(context, argPos, services)); Task.Run(async () => await commands.ExecuteAsync(context, argPos, servicesProvider));
} }
public async Task HandleMessageReceived(SocketMessage messsageParam) public async Task HandleMessageReceived(SocketMessage messsageParam)
@ -152,7 +157,7 @@ namespace Geekbot.net
{ {
if (!user.IsBot) if (!user.IsBot)
{ {
var message = redis.Client.StringGet(user.Guild.Id + "-welcomeMsg"); var message = redis.StringGet(user.Guild.Id + "-welcomeMsg");
if (!message.IsNullOrEmpty) if (!message.IsNullOrEmpty)
{ {
message = message.ToString().Replace("$user", user.Mention); message = message.ToString().Replace("$user", user.Mention);