From caf8bcc761feb85c9cfde74189f644be712f29be Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 31 Aug 2016 07:49:32 +0200 Subject: [PATCH] Cross server almost done, need owner-only permission check --- .../Commands/CrossServerTextChannel.cs | 200 +++++++++--------- .../_Models/JSONModels/AnimeResult.cs | 20 ++ 2 files changed, 116 insertions(+), 104 deletions(-) create mode 100644 src/NadekoBot/_Models/JSONModels/AnimeResult.cs diff --git a/src/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs b/src/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs index 5c0bd358..d68b2f78 100644 --- a/src/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs +++ b/src/NadekoBot/Modules/Administration/Commands/CrossServerTextChannel.cs @@ -1,111 +1,103 @@ -//using Discord; -//using Discord.Commands; -//using NadekoBot.Classes; -//using NadekoBot.Modules.Permissions.Classes; -//using System; -//using System.Collections.Concurrent; -//using System.Collections.Generic; -//using System.Linq; +using Discord; +using Discord.Commands; +using Discord.WebSocket; +using NadekoBot.Attributes; +using NadekoBot.Extensions; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; -////todo DB -//namespace NadekoBot.Modules.Administration -//{ -// class CrossServerTextChannel : DiscordCommand -// { -// public CrossServerTextChannel(DiscordModule module) : base(module) -// { -// NadekoBot.Client.MessageReceived += async (s, e) => -// { -// try -// { -// if (umsg.Author.Id == NadekoBot.Client.CurrentUser.Id) return; -// foreach (var subscriber in Subscribers) -// { -// var set = subscriber.Value; -// if (!set.Contains(e.Channel)) -// continue; -// foreach (var chan in set.Except(new[] { e.Channel })) -// { -// await chan.SendMessageAsync(GetText(e.Server, e.Channel, umsg.Author, e.Message)).ConfigureAwait(false); -// } -// } -// } -// catch { } -// }; -// NadekoBot.Client.MessageUpdated += async (s, e) => -// { -// try -// { -// if (e.After?.User?.Id == null || e.After.User.Id == NadekoBot.Client.CurrentUser.Id) return; -// foreach (var subscriber in Subscribers) -// { -// var set = subscriber.Value; -// if (!set.Contains(e.Channel)) -// continue; -// foreach (var chan in set.Except(new[] { e.Channel })) -// { -// var msg = chan.Messages -// .FirstOrDefault(m => -// m.RawText == GetText(e.Server, e.Channel, umsg.Author, e.Before)); -// if (msg != default(Message)) -// await msg.Edit(GetText(e.Server, e.Channel, umsg.Author, e.After)).ConfigureAwait(false); -// } -// } +namespace NadekoBot.Modules.Administration +{ + public partial class Administration + { + [Group] + public class CrossServerTextChannel + { + public CrossServerTextChannel() + { + NadekoBot.Client.MessageReceived += (imsg) => + { + var msg = imsg as IUserMessage; + if (msg == null) + return Task.CompletedTask; -// } -// catch { } -// }; -// } + var channel = imsg.Channel as ITextChannel; + if (channel == null) + return Task.CompletedTask; -// private string GetText(Server server, Channel channel, User user, Message message) => -// $"**{server.Name} | {channel.Name}** `{user.Name}`: " + message.RawText; + Task.Run(async () => + { + try + { + if (msg.Author.Id == NadekoBot.Client.GetCurrentUser().Id) return; + foreach (var subscriber in Subscribers) + { + var set = subscriber.Value; + if (!set.Contains(msg.Channel)) + continue; + foreach (var chan in set.Except(new[] { channel })) + { + await chan.SendMessageAsync(GetText(channel.Guild, channel, (IGuildUser)msg.Author, msg)).ConfigureAwait(false); + } + } + } + catch { } + }); -// public static readonly ConcurrentDictionary> Subscribers = new ConcurrentDictionary>(); + return Task.CompletedTask; + }; + } -// internal override void Init(CommandGroupBuilder cgb) -// { -// cgb.CreateCommand(Module.Prefix + "scsc") -// .Description("Starts an instance of cross server channel. You will get a token as a DM " + -// $"that other people will use to tune in to the same instance. **Bot Owner Only.** | `{Prefix}scsc`") -// .AddCheck(SimpleCheckers.OwnerOnly()) -// .Do(async e => -// { -// var token = new Random().Next(); -// var set = new HashSet(); -// if (Subscribers.TryAdd(token, set)) -// { -// set.Add(e.Channel); -// await umsg.Author.SendMessageAsync("This is your CSC token:" + token.ToString()).ConfigureAwait(false); -// } -// }); + private string GetText(IGuild server, ITextChannel channel, IGuildUser user, IUserMessage message) => + $"**{server.Name} | {channel.Name}** `{user.Username}`: " + message.Content; -// cgb.CreateCommand(Module.Prefix + "jcsc") -// .Description($"Joins current channel to an instance of cross server channel using the token. **Needs Manage Server Permissions.**| `{Prefix}jcsc`") -// .Parameter("token") -// .AddCheck(SimpleCheckers.ManageServer()) -// .Do(async e => -// { -// int token; -// if (!int.TryParse(token, out token)) -// return; -// HashSet set; -// if (!Subscribers.TryGetValue(token, out set)) -// return; -// set.Add(e.Channel); -// await channel.SendMessageAsync(":ok:").ConfigureAwait(false); -// }); + public static readonly ConcurrentDictionary> Subscribers = new ConcurrentDictionary>(); -// cgb.CreateCommand(Module.Prefix + "lcsc") -// .Description($"Leaves Cross server channel instance from this channel. **Needs Manage Server Permissions.**| `{Prefix}lcsc`") -// .AddCheck(SimpleCheckers.ManageServer()) -// .Do(async e => -// { -// foreach (var subscriber in Subscribers) -// { -// subscriber.Value.Remove(e.Channel); -// } -// await channel.SendMessageAsync(":ok:").ConfigureAwait(false); -// }); -// } -// } -//} + //todo owner only + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + public async Task Scsc(IUserMessage msg) + { + var channel = (ITextChannel)msg.Channel; + var token = new Random().Next(); + var set = new HashSet(); + if (Subscribers.TryAdd(token, set)) + { + set.Add(channel); + await ((IGuildUser)msg.Author).SendMessageAsync("This is your CSC token:" + token.ToString()).ConfigureAwait(false); + } + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + [RequirePermission(GuildPermission.ManageGuild)] + public async Task Jcsc(IUserMessage imsg, int token) + { + var channel = (ITextChannel)imsg.Channel; + + HashSet set; + if (!Subscribers.TryGetValue(token, out set)) + return; + set.Add(channel); + await channel.SendMessageAsync(":ok:").ConfigureAwait(false); + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + [RequirePermission(GuildPermission.ManageGuild)] + public async Task cmd(IUserMessage imsg) + { + var channel = (ITextChannel)imsg.Channel; + + foreach (var subscriber in Subscribers) + { + subscriber.Value.Remove(channel); + } + await channel.SendMessageAsync(":ok:").ConfigureAwait(false); + } + } + } +} \ No newline at end of file diff --git a/src/NadekoBot/_Models/JSONModels/AnimeResult.cs b/src/NadekoBot/_Models/JSONModels/AnimeResult.cs new file mode 100644 index 00000000..a092ae03 --- /dev/null +++ b/src/NadekoBot/_Models/JSONModels/AnimeResult.cs @@ -0,0 +1,20 @@ +namespace NadekoBot.Classes.JSONModels +{ + public class AnimeResult + { + public int id; + public string airing_status; + public string title_english; + public int total_episodes; + public string description; + public string image_url_lge; + + public override string ToString() => + "`Title:` **" + title_english + + "**\n`Status:` " + airing_status + + "\n`Episodes:` " + total_episodes + + "\n`Link:` http://anilist.co/anime/" + id + + "\n`Synopsis:` " + description.Substring(0, description.Length > 500 ? 500 : description.Length) + "..." + + "\n`img:` " + image_url_lge; + } +} \ No newline at end of file