Fix first argument

This commit is contained in:
Kwoth 2016-08-29 14:34:46 +02:00
parent 248dab4855
commit 687fedca75
6 changed files with 68 additions and 24 deletions

View File

@ -1,5 +1,6 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
@ -8,6 +9,7 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
//todo DB //todo DB
@ -19,43 +21,87 @@ namespace NadekoBot.Modules.Administration
[Group] [Group]
public class RepeatCommands public class RepeatCommands
{ {
public ConcurrentDictionary<ulong, Repeater> repeaters; public ConcurrentDictionary<ulong, RepeatRunner> repeaters;
public class RepeatRunner
{
private CancellationTokenSource source { get; set; }
private CancellationToken token { get; set; }
public Repeater Repeater { get; }
public ITextChannel Channel { get; }
public RepeatRunner(Repeater repeater)
{
this.Repeater = repeater;
this.Channel = NadekoBot.Client.GetGuild(repeater.GuildId)?.GetTextChannel(repeater.ChannelId);
if (Channel == null)
return;
Task.Run(Run);
}
private async Task Run()
{
source = new CancellationTokenSource();
token = source.Token;
try
{
while (!token.IsCancellationRequested)
{
await Task.Delay(Repeater.Interval, token).ConfigureAwait(false);
await Channel.SendMessageAsync("🔄 " + Repeater.Message).ConfigureAwait(false);
}
}
catch (OperationCanceledException) { }
}
public void Reset()
{
source.Cancel();
var t = Task.Run(Run);
}
public void Stop()
{
source.Cancel();
}
}
public RepeatCommands() public RepeatCommands()
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
repeaters = new ConcurrentDictionary<ulong, Repeater>(uow.Repeaters.GetAll().ToDictionary(r => r.ChannelId)); repeaters = new ConcurrentDictionary<ulong, RepeatRunner>(uow.Repeaters.GetAll().Select(r => new RepeatRunner(r)).Where(r => r != null).ToDictionary(r => r.Repeater.ChannelId));
} }
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary] [LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageMessages)] [RequirePermission(GuildPermission.ManageMessages)]
public async Task RepeatInvoke(IMessage imsg) public async Task RepeatInvoke(IUserMessage imsg)
{ {
var channel = (ITextChannel)imsg.Channel; var channel = (ITextChannel)imsg.Channel;
Repeater rep; RepeatRunner rep;
if (!repeaters.TryGetValue(channel.Id, out rep)) if (!repeaters.TryGetValue(channel.Id, out rep))
{ {
await channel.SendMessageAsync("`No repeating message found on this server.`").ConfigureAwait(false); await channel.SendMessageAsync("`No repeating message found on this server.`").ConfigureAwait(false);
return; return;
} }
rep.Reset();
await channel.SendMessageAsync("🔄 " + rep.Message); await channel.SendMessageAsync("🔄 " + rep.Repeater.Message);
} }
[LocalizedCommand, LocalizedDescription, LocalizedSummary] [LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task Repeat(IMessage imsg, int minutes, [Remainder] string message = null) public async Task Repeat(IUserMessage imsg, int minutes, [Remainder] string message = null)
{ {
var channel = (ITextChannel)imsg.Channel; var channel = (ITextChannel)imsg.Channel;
if (minutes < 1 || minutes > 1500) if (minutes < 1 || minutes > 1500)
return; return;
Repeater rep; RepeatRunner rep;
if (string.IsNullOrWhiteSpace(message)) //turn off if (string.IsNullOrWhiteSpace(message)) //turn off
{ {
@ -63,9 +109,10 @@ namespace NadekoBot.Modules.Administration
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
uow.Repeaters.Remove(rep); uow.Repeaters.Remove(rep.Repeater);
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
rep.Stop();
await channel.SendMessageAsync("`Stopped repeating a message.`").ConfigureAwait(false); await channel.SendMessageAsync("`Stopped repeating a message.`").ConfigureAwait(false);
} }
else else
@ -86,18 +133,19 @@ namespace NadekoBot.Modules.Administration
}; };
uow.Repeaters.Add(localRep); uow.Repeaters.Add(localRep);
uow.Complete(); uow.Complete();
return localRep; return new RepeatRunner(localRep);
} }
}, (cid, old) => }, (cid, old) =>
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
old.Message = message; old.Repeater.Message = message;
old.Interval = TimeSpan.FromMinutes(minutes); old.Repeater.Interval = TimeSpan.FromMinutes(minutes);
uow.Repeaters.Update(old); uow.Repeaters.Update(old.Repeater);
uow.Complete(); uow.Complete();
return old;
} }
old.Reset();
return old;
}); });
} }

View File

@ -338,7 +338,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
if (string.IsNullOrWhiteSpace(usrStr)) if (string.IsNullOrWhiteSpace(usrStr))
return; return;
var usr = (await channel.Guild.GetUsersAsync()).Where(u => u.Username.ToUpperInvariant() == usrStr).FirstOrDefault(); var usr = channel.Guild.GetUsers().Where(u => u.Username.ToUpperInvariant() == usrStr).FirstOrDefault();
if (usr == null || string.IsNullOrWhiteSpace(usr.AvatarUrl)) if (usr == null || string.IsNullOrWhiteSpace(usr.AvatarUrl))
return; return;

View File

@ -1,5 +1,6 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System; using System;
@ -21,7 +22,7 @@ namespace NadekoBot.Modules.Utility
if (guild == null) if (guild == null)
server = channel.Guild; server = channel.Guild;
else else
server = (await _client.GetGuildsAsync()).Where(g => g.Name.ToUpperInvariant() == guild.ToUpperInvariant()).FirstOrDefault(); server = _client.GetGuilds().Where(g => g.Name.ToUpperInvariant() == guild.ToUpperInvariant()).FirstOrDefault();
if (server == null) if (server == null)
return; return;

View File

@ -67,12 +67,7 @@ namespace NadekoBot.Modules.Utility
} }
else else
{ {
ch = NadekoBot.Client.GetGuilds() ch = NadekoBot.Client.GetGuild(r.ServerId)?.GetTextChannel(r.ChannelId);
.Where(g => g.Id == r.ServerId)
.FirstOrDefault()
.GetTextChannels()
.Where(c => c.Id == r.ChannelId)
.FirstOrDefault();
} }
if (ch == null) if (ch == null)
return; return;

View File

@ -34,7 +34,7 @@ namespace NadekoBot.Services.Impl
public Task<string> Print() => Task.FromResult($@"`Author: Kwoth` `Library: Discord.Net` public Task<string> Print() => Task.FromResult($@"`Author: Kwoth` `Library: Discord.Net`
`Bot Version: {BotVersion}` `Bot Version: {BotVersion}`
`Bot id: {(client.GetCurrentUser()).Id}` `Bot id: {(client.GetCurrentUser()).Id}`
`Owners' Ids:` `Owners' Ids: {string.Join(", ", NadekoBot.Credentials.OwnerIds)}`
`Uptime: {GetUptimeString()}` `Uptime: {GetUptimeString()}`
`Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()}` `Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()}`
`Messages: {messageCounter} ({messageCounter / (double)GetUptime().TotalSeconds:F2}/sec)` `Heap: {Heap} MB`"); `Messages: {messageCounter} ({messageCounter / (double)GetUptime().TotalSeconds:F2}/sec)` `Heap: {Heap} MB`");

View File

@ -40,7 +40,7 @@ namespace NadekoBot.Extensions
Task.FromResult(NadekoBot.Client.GetCurrentUser().Id == msg.Author.Id); Task.FromResult(NadekoBot.Client.GetCurrentUser().Id == msg.Author.Id);
public static IEnumerable<IUser> Members(this IRole role) => public static IEnumerable<IUser> Members(this IRole role) =>
NadekoBot.Client.GetGuilds().Where(g => g.Id == role.GuildId).FirstOrDefault()?.GetUsers().Where(u => u.Roles.Contains(role)) ?? Enumerable.Empty<IUser>(); NadekoBot.Client.GetGuild(role.GuildId)?.GetUsers().Where(u => u.Roles.Contains(role)) ?? Enumerable.Empty<IUser>();
public static async Task<IUserMessage[]> ReplyLong(this IUserMessage msg, string content, string breakOn = "\n", string addToEnd = "", string addToStart = "") public static async Task<IUserMessage[]> ReplyLong(this IUserMessage msg, string content, string breakOn = "\n", string addToEnd = "", string addToStart = "")
{ {