Refactored Music, NFW, Search, Trello, ClashOfClans Modules ...

This commit is contained in:
Master Kwoth 2016-04-14 23:47:02 +02:00
parent eb90bb5bd1
commit 4bc7ec9d47
12 changed files with 80 additions and 57 deletions

View File

@ -1,8 +1,8 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Modules;
using NadekoBot.Modules.Administration.Commands; using NadekoBot.Modules.Administration.Commands;
using NadekoBot.Modules.Music;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -139,7 +139,7 @@ namespace NadekoBot
public Task LoadStats() => public Task LoadStats() =>
Task.Run(() => Task.Run(() =>
{ {
var songs = Music.MusicPlayers.Count(mp => mp.Value.CurrentSong != null); var songs = MusicModule.MusicPlayers.Count(mp => mp.Value.CurrentSong != null);
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
sb.AppendLine("`Author: Kwoth` `Library: Discord.Net`"); sb.AppendLine("`Author: Kwoth` `Library: Discord.Net`");
sb.AppendLine($"`Bot Version: {BotVersion}`"); sb.AppendLine($"`Bot Version: {BotVersion}`");
@ -154,7 +154,7 @@ namespace NadekoBot
sb.AppendLine($"`Message queue size: {NadekoBot.Client.MessageQueue.Count}`"); sb.AppendLine($"`Message queue size: {NadekoBot.Client.MessageQueue.Count}`");
sb.Append($"`Greeted {ServerGreetCommand.Greeted} times.`"); sb.Append($"`Greeted {ServerGreetCommand.Greeted} times.`");
sb.AppendLine($" `| Playing {songs} songs, ".SnPl(songs) + sb.AppendLine($" `| Playing {songs} songs, ".SnPl(songs) +
$"{Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count)} queued.`"); $"{MusicModule.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count)} queued.`");
sb.AppendLine($"`Heap: {Heap(false)}`"); sb.AppendLine($"`Heap: {Heap(false)}`");
statsCache = sb.ToString(); statsCache = sb.ToString();
}); });

View File

@ -2,6 +2,7 @@
using NadekoBot.Classes.JSONModels; using NadekoBot.Classes.JSONModels;
using NadekoBot.Commands; using NadekoBot.Commands;
using NadekoBot.Modules.Games.Commands; using NadekoBot.Modules.Games.Commands;
using NadekoBot.Modules.Music;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -20,10 +21,10 @@ namespace NadekoBot.Modules.Administration.Commands
{"%servers%", () => NadekoBot.Client.Servers.Count().ToString()}, {"%servers%", () => NadekoBot.Client.Servers.Count().ToString()},
{"%users%", () => NadekoBot.Client.Servers.SelectMany(s => s.Users).Count().ToString()}, {"%users%", () => NadekoBot.Client.Servers.SelectMany(s => s.Users).Count().ToString()},
{"%playing%", () => { {"%playing%", () => {
var cnt = Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); var cnt = MusicModule.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
if (cnt != 1) return cnt.ToString(); if (cnt != 1) return cnt.ToString();
try { try {
var mp = Music.MusicPlayers.FirstOrDefault(); var mp = MusicModule.MusicPlayers.FirstOrDefault();
return mp.Value.CurrentSong.SongInfo.Title; return mp.Value.CurrentSong.SongInfo.Title;
} }
catch { catch {
@ -31,7 +32,7 @@ namespace NadekoBot.Modules.Administration.Commands
} }
} }
}, },
{"%queued%", () => Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, {"%queued%", () => MusicModule.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()},
{"%trivia%", () => Trivia.RunningTrivias.Count.ToString()} {"%trivia%", () => Trivia.RunningTrivias.Count.ToString()}
}; };

View File

@ -1,15 +1,14 @@
using Discord.Commands; using Discord.Commands;
using Discord.Modules; using Discord.Modules;
using NadekoBot.Classes.ClashOfClans; using NadekoBot.Classes.ClashOfClans;
using NadekoBot.Modules;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
namespace NadekoBot.Commands namespace NadekoBot.Modules.ClashOfClans
{ {
internal class ClashOfClans : DiscordModule internal class ClashOfClansModule : DiscordModule
{ {
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.ClashOfClans; public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.ClashOfClans;

View File

@ -6,10 +6,10 @@ using System.Linq;
namespace NadekoBot.Modules.Help namespace NadekoBot.Modules.Help
{ {
internal class Help : DiscordModule internal class HelpModule : DiscordModule
{ {
public Help() public HelpModule()
{ {
commands.Add(new HelpCommand(this)); commands.Add(new HelpCommand(this));
} }

View File

@ -13,15 +13,15 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Modules namespace NadekoBot.Modules.Music
{ {
internal class Music : DiscordModule internal class MusicModule : DiscordModule
{ {
public static ConcurrentDictionary<Server, MusicPlayer> MusicPlayers = new ConcurrentDictionary<Server, MusicPlayer>(); public static ConcurrentDictionary<Server, MusicPlayer> MusicPlayers = new ConcurrentDictionary<Server, MusicPlayer>();
public static ConcurrentDictionary<ulong, float> DefaultMusicVolumes = new ConcurrentDictionary<ulong, float>(); public static ConcurrentDictionary<ulong, float> DefaultMusicVolumes = new ConcurrentDictionary<ulong, float>();
public Music() public MusicModule()
{ {
// ready for 1.0 // ready for 1.0
//NadekoBot.Client.UserUpdated += (s, e) => //NadekoBot.Client.UserUpdated += (s, e) =>

View File

@ -4,9 +4,9 @@ using NadekoBot.Classes;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
namespace NadekoBot.Modules namespace NadekoBot.Modules.NSFW
{ {
internal class NSFW : DiscordModule internal class NSFWModule : DiscordModule
{ {
private readonly Random rng = new Random(); private readonly Random rng = new Random();

View File

@ -306,7 +306,7 @@ Assists: {general["assists"]} Ban: {general["banRate"]}%
await e.Channel.SendMessage(sb.ToString()); await e.Channel.SendMessage(sb.ToString());
} }
catch (Exception ex) catch (Exception)
{ {
await e.Channel.SendMessage($":anger: Fail: Champion.gg didsabled ban data until next patch. Sorry for the inconvenience."); await e.Channel.SendMessage($":anger: Fail: Champion.gg didsabled ban data until next patch. Sorry for the inconvenience.");
} }

View File

@ -16,10 +16,10 @@ using System.Net;
namespace NadekoBot.Modules.Searches namespace NadekoBot.Modules.Searches
{ {
internal class Searches : DiscordModule internal class SearchesModule : DiscordModule
{ {
private readonly Random rng; private readonly Random rng;
public Searches() public SearchesModule()
{ {
commands.Add(new LoLCommands(this)); commands.Add(new LoLCommands(this));
commands.Add(new StreamNotifications(this)); commands.Add(new StreamNotifications(this));

View File

@ -1,19 +1,22 @@
using System; using Discord.Modules;
using Manatee.Trello;
using Manatee.Trello.ManateeJson;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Discord.Modules;
using Manatee.Trello.ManateeJson;
using Manatee.Trello;
using System.Timers; using System.Timers;
using NadekoBot.Extensions;
using Action = Manatee.Trello.Action; using Action = Manatee.Trello.Action;
namespace NadekoBot.Modules { namespace NadekoBot.Modules.Trello
internal class Trello : DiscordModule { {
internal class TrelloModule : DiscordModule
{
private readonly Timer t = new Timer { Interval = 2000 }; private readonly Timer t = new Timer { Interval = 2000 };
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Trello; public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Trello;
public override void Install(ModuleManager manager) { public override void Install(ModuleManager manager)
{
var client = manager.Client; var client = manager.Client;
@ -29,8 +32,10 @@ namespace NadekoBot.Modules {
Board board = null; Board board = null;
List<string> last5ActionIDs = null; List<string> last5ActionIDs = null;
t.Elapsed += async (s, e) => { t.Elapsed += async (s, e) =>
try { {
try
{
if (board == null || bound == null) if (board == null || bound == null)
return; //do nothing if there is no bound board return; //do nothing if there is no bound board
@ -38,22 +43,27 @@ namespace NadekoBot.Modules {
var cur5Actions = board.Actions.Take(board.Actions.Count() < 5 ? board.Actions.Count() : 5); var cur5Actions = board.Actions.Take(board.Actions.Count() < 5 ? board.Actions.Count() : 5);
var cur5ActionsArray = cur5Actions as Action[] ?? cur5Actions.ToArray(); var cur5ActionsArray = cur5Actions as Action[] ?? cur5Actions.ToArray();
if (last5ActionIDs == null) { if (last5ActionIDs == null)
{
last5ActionIDs = cur5ActionsArray.Select(a => a.Id).ToList(); last5ActionIDs = cur5ActionsArray.Select(a => a.Id).ToList();
return; return;
} }
foreach (var a in cur5ActionsArray.Where(ca => !last5ActionIDs.Contains(ca.Id))) { foreach (var a in cur5ActionsArray.Where(ca => !last5ActionIDs.Contains(ca.Id)))
{
await bound.Send("**--TRELLO NOTIFICATION--**\n" + a.ToString()); await bound.Send("**--TRELLO NOTIFICATION--**\n" + a.ToString());
} }
last5ActionIDs.Clear(); last5ActionIDs.Clear();
last5ActionIDs.AddRange(cur5ActionsArray.Select(a => a.Id)); last5ActionIDs.AddRange(cur5ActionsArray.Select(a => a.Id));
} catch (Exception ex) { }
catch (Exception ex)
{
Console.WriteLine("Timer failed " + ex.ToString()); Console.WriteLine("Timer failed " + ex.ToString());
} }
}; };
manager.CreateCommands("trello ", cgb => { manager.CreateCommands("trello ", cgb =>
{
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
@ -61,11 +71,15 @@ namespace NadekoBot.Modules {
.Alias("j") .Alias("j")
.Description("Joins a server") .Description("Joins a server")
.Parameter("code", Discord.Commands.ParameterType.Required) .Parameter("code", Discord.Commands.ParameterType.Required)
.Do(async e => { .Do(async e =>
{
if (!NadekoBot.IsOwner(e.User.Id)) return; if (!NadekoBot.IsOwner(e.User.Id)) return;
try { try
{
await (await client.GetInvite(e.GetArg("code"))).Accept(); await (await client.GetInvite(e.GetArg("code"))).Accept();
} catch (Exception ex) { }
catch (Exception ex)
{
Console.WriteLine(ex.ToString()); Console.WriteLine(ex.ToString());
} }
}); });
@ -75,23 +89,28 @@ namespace NadekoBot.Modules {
"You will receive notifications from your board when something is added or edited." + "You will receive notifications from your board when something is added or edited." +
"\n**Usage**: bind [board_id]") "\n**Usage**: bind [board_id]")
.Parameter("board_id", Discord.Commands.ParameterType.Required) .Parameter("board_id", Discord.Commands.ParameterType.Required)
.Do(async e => { .Do(async e =>
{
if (!NadekoBot.IsOwner(e.User.Id)) return; if (!NadekoBot.IsOwner(e.User.Id)) return;
if (bound != null) return; if (bound != null) return;
try { try
{
bound = e.Channel; bound = e.Channel;
board = new Board(e.GetArg("board_id").Trim()); board = new Board(e.GetArg("board_id").Trim());
board.Refresh(); board.Refresh();
await e.Channel.SendMessage("Successfully bound to this channel and board " + board.Name); await e.Channel.SendMessage("Successfully bound to this channel and board " + board.Name);
t.Start(); t.Start();
} catch (Exception ex) { }
catch (Exception ex)
{
Console.WriteLine("Failed to join the board. " + ex.ToString()); Console.WriteLine("Failed to join the board. " + ex.ToString());
} }
}); });
cgb.CreateCommand("unbind") cgb.CreateCommand("unbind")
.Description("Unbinds a bot from the channel and board.") .Description("Unbinds a bot from the channel and board.")
.Do(async e => { .Do(async e =>
{
if (!NadekoBot.IsOwner(e.User.Id)) return; if (!NadekoBot.IsOwner(e.User.Id)) return;
if (bound == null || bound != e.Channel) return; if (bound == null || bound != e.Channel) return;
t.Stop(); t.Stop();
@ -104,7 +123,8 @@ namespace NadekoBot.Modules {
cgb.CreateCommand("lists") cgb.CreateCommand("lists")
.Alias("list") .Alias("list")
.Description("Lists all lists yo ;)") .Description("Lists all lists yo ;)")
.Do(async e => { .Do(async e =>
{
if (!NadekoBot.IsOwner(e.User.Id)) return; if (!NadekoBot.IsOwner(e.User.Id)) return;
if (bound == null || board == null || bound != e.Channel) return; if (bound == null || board == null || bound != e.Channel) return;
await e.Channel.SendMessage("Lists for a board '" + board.Name + "'\n" + string.Join("\n", board.Lists.Select(l => "**• " + l.ToString() + "**"))); await e.Channel.SendMessage("Lists for a board '" + board.Name + "'\n" + string.Join("\n", board.Lists.Select(l => "**• " + l.ToString() + "**")));
@ -113,7 +133,8 @@ namespace NadekoBot.Modules {
cgb.CreateCommand("cards") cgb.CreateCommand("cards")
.Description("Lists all cards from the supplied list. You can supply either a name or an index.") .Description("Lists all cards from the supplied list. You can supply either a name or an index.")
.Parameter("list_name", Discord.Commands.ParameterType.Unparsed) .Parameter("list_name", Discord.Commands.ParameterType.Unparsed)
.Do(async e => { .Do(async e =>
{
if (!NadekoBot.IsOwner(e.User.Id)) return; if (!NadekoBot.IsOwner(e.User.Id)) return;
if (bound == null || board == null || bound != e.Channel || e.GetArg("list_name") == null) return; if (bound == null || board == null || bound != e.Channel || e.GetArg("list_name") == null) return;

View File

@ -3,19 +3,21 @@ using Discord.Audio;
using Discord.Commands; using Discord.Commands;
using Discord.Modules; using Discord.Modules;
using NadekoBot.Classes.JSONModels; using NadekoBot.Classes.JSONModels;
using NadekoBot.Commands;
using NadekoBot.Commands.Help.Commands; using NadekoBot.Commands.Help.Commands;
using NadekoBot.Modules;
using NadekoBot.Modules.Administration; using NadekoBot.Modules.Administration;
using NadekoBot.Modules.ClashOfClans;
using NadekoBot.Modules.Conversations; using NadekoBot.Modules.Conversations;
using NadekoBot.Modules.Gambling; using NadekoBot.Modules.Gambling;
using NadekoBot.Modules.Games; using NadekoBot.Modules.Games;
using NadekoBot.Modules.Games.Commands; using NadekoBot.Modules.Games.Commands;
using NadekoBot.Modules.Help; using NadekoBot.Modules.Help;
using NadekoBot.Modules.Music;
using NadekoBot.Modules.NSFW;
using NadekoBot.Modules.Permissions; using NadekoBot.Modules.Permissions;
using NadekoBot.Modules.Pokemon; using NadekoBot.Modules.Pokemon;
using NadekoBot.Modules.Searches; using NadekoBot.Modules.Searches;
using NadekoBot.Modules.Translator; using NadekoBot.Modules.Translator;
using NadekoBot.Modules.Trello;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -170,19 +172,19 @@ namespace NadekoBot
//install modules //install modules
modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None); modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None);
modules.Add(new Help(), "Help", ModuleFilter.None); modules.Add(new HelpModule(), "Help", ModuleFilter.None);
modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None); modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None);
modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None); modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None);
modules.Add(new GamesModule(), "Games", ModuleFilter.None); modules.Add(new GamesModule(), "Games", ModuleFilter.None);
modules.Add(new Music(), "Music", ModuleFilter.None); modules.Add(new MusicModule(), "Music", ModuleFilter.None);
modules.Add(new Searches(), "Searches", ModuleFilter.None); modules.Add(new SearchesModule(), "Searches", ModuleFilter.None);
modules.Add(new NSFW(), "NSFW", ModuleFilter.None); modules.Add(new NSFWModule(), "NSFW", ModuleFilter.None);
modules.Add(new ClashOfClans(), "ClashOfClans", ModuleFilter.None); modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None);
modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None); modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None);
modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None); modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None);
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey)) if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))
modules.Add(new Trello(), "Trello", ModuleFilter.None); modules.Add(new TrelloModule(), "Trello", ModuleFilter.None);
//run the bot //run the bot
Client.ExecuteAndWait(async () => Client.ExecuteAndWait(async () =>

View File

@ -120,7 +120,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Classes\ClashOfClans\ClashOfClans.cs" /> <Compile Include="Modules\ClashOfClans\ClashOfClans.cs" />
<Compile Include="Classes\DBHandler.cs" /> <Compile Include="Classes\DBHandler.cs" />
<Compile Include="Classes\FlowersHandler.cs" /> <Compile Include="Classes\FlowersHandler.cs" />
<Compile Include="Classes\IMDB\ImdbMovie.cs" /> <Compile Include="Classes\IMDB\ImdbMovie.cs" />
@ -167,7 +167,7 @@
<Compile Include="Modules\Administration\Commands\InfoCommands.cs" /> <Compile Include="Modules\Administration\Commands\InfoCommands.cs" />
<Compile Include="Modules\Administration\Commands\Remind.cs" /> <Compile Include="Modules\Administration\Commands\Remind.cs" />
<Compile Include="Modules\Administration\Commands\SelfAssignedRolesCommand.cs" /> <Compile Include="Modules\Administration\Commands\SelfAssignedRolesCommand.cs" />
<Compile Include="Modules\ClashOfClans.cs" /> <Compile Include="Modules\ClashOfClans\ClashOfClansModule.cs" />
<Compile Include="Modules\Permissions\Commands\FilterWordsCommand.cs" /> <Compile Include="Modules\Permissions\Commands\FilterWordsCommand.cs" />
<Compile Include="Modules\Permissions\Commands\FilterInvitesCommand.cs" /> <Compile Include="Modules\Permissions\Commands\FilterInvitesCommand.cs" />
<Compile Include="Modules\Administration\Commands\LogCommand.cs" /> <Compile Include="Modules\Administration\Commands\LogCommand.cs" />
@ -197,22 +197,22 @@
<Compile Include="Modules\Gambling\GamblingModule.cs" /> <Compile Include="Modules\Gambling\GamblingModule.cs" />
<Compile Include="Modules\Games\Commands\Bomberman.cs" /> <Compile Include="Modules\Games\Commands\Bomberman.cs" />
<Compile Include="Modules\Games\GamesModule.cs" /> <Compile Include="Modules\Games\GamesModule.cs" />
<Compile Include="Modules\Help\Help.cs" /> <Compile Include="Modules\Help\HelpModule.cs" />
<Compile Include="Modules\Music.cs" /> <Compile Include="Modules\Music\MusicModule.cs" />
<Compile Include="Modules\Games\Commands\PollCommand.cs" /> <Compile Include="Modules\Games\Commands\PollCommand.cs" />
<Compile Include="Modules\NSFW.cs" /> <Compile Include="Modules\NSFW\NSFWModule.cs" />
<Compile Include="Modules\Permissions\PermissionsModule.cs" /> <Compile Include="Modules\Permissions\PermissionsModule.cs" />
<Compile Include="Modules\Administration\Commands\RatelimitCommand.cs" /> <Compile Include="Modules\Administration\Commands\RatelimitCommand.cs" />
<Compile Include="Modules\Pokemon\PokemonModule.cs" /> <Compile Include="Modules\Pokemon\PokemonModule.cs" />
<Compile Include="Modules\Pokemon\PokeStats.cs" /> <Compile Include="Modules\Pokemon\PokeStats.cs" />
<Compile Include="Modules\Searches\Searches.cs" /> <Compile Include="Modules\Searches\SearchesModule.cs" />
<Compile Include="Modules\Searches\Commands\ConverterCommand.cs" /> <Compile Include="Modules\Searches\Commands\ConverterCommand.cs" />
<Compile Include="Modules\Searches\Commands\RedditCommand.cs" /> <Compile Include="Modules\Searches\Commands\RedditCommand.cs" />
<Compile Include="Modules\Translator\Helpers\GoogleTranslator.cs" /> <Compile Include="Modules\Translator\Helpers\GoogleTranslator.cs" />
<Compile Include="Modules\Translator\TranslateCommand.cs" /> <Compile Include="Modules\Translator\TranslateCommand.cs" />
<Compile Include="Modules\Translator\TranslatorModule.cs" /> <Compile Include="Modules\Translator\TranslatorModule.cs" />
<Compile Include="Modules\Translator\ValidLanguagesCommand.cs" /> <Compile Include="Modules\Translator\ValidLanguagesCommand.cs" />
<Compile Include="Modules\Trello.cs" /> <Compile Include="Modules\Trello\TrelloModule.cs" />
<Compile Include="NadekoBot.cs" /> <Compile Include="NadekoBot.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">