Fixed memory leak, trivia hints are after 6 sec, etc

This commit is contained in:
Master Kwoth
2016-01-30 05:24:32 +01:00
parent 1316c83bd4
commit 00f09b66bd
11 changed files with 178 additions and 209 deletions

View File

@ -10,17 +10,14 @@ using System.Threading.Tasks;
using NadekoBot.Commands;
using System.IO;
namespace NadekoBot.Modules
{
class Administration : DiscordModule
{
namespace NadekoBot.Modules {
class Administration : DiscordModule {
public Administration() : base() {
commands.Add(new HelpCommand());
commands.Add(new ServerGreetCommand());
}
public override void Install(ModuleManager manager)
{
public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => {
var client = manager.Client;
@ -133,22 +130,22 @@ namespace NadekoBot.Modules
try {
bool rgb = args.Count() == 4;
byte red = Convert.ToByte(rgb ? int.Parse(e.Args[1]) : Convert.ToInt32(e.Args[1].Substring(0,2), 16));
byte red = Convert.ToByte(rgb ? int.Parse(e.Args[1]) : Convert.ToInt32(e.Args[1].Substring(0, 2), 16));
byte green = Convert.ToByte(rgb ? int.Parse(e.Args[2]) : Convert.ToInt32(e.Args[1].Substring(2, 2), 16));
byte blue = Convert.ToByte(rgb ? int.Parse(e.Args[3]) : Convert.ToInt32(e.Args[1].Substring(4, 2), 16));
await role.Edit(color: new Color(red, green, blue));
await e.Channel.SendMessage($"Role {role.Name}'s color has been changed.");
} catch (Exception ex) {
await e.Send(":warning: Unspecified error, please report this.");
Console.WriteLine($".rolecolor error: {ex}");
}
});
cgb.CreateCommand(".b").Alias(".ban")
.Parameter("everything",ParameterType.Unparsed)
.Parameter("everything", ParameterType.Unparsed)
.Description("Bans a mentioned user")
.Do(async e => {
try {
@ -259,7 +256,7 @@ namespace NadekoBot.Modules
.Parameter("user", ParameterType.Optional)
.Do(async e => {
var usr = e.User;
if(e.GetArg("user") != null) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
if (e.GetArg("user") != null) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
await e.Send($"Id of the user { usr.Name } is { usr.Id }");
});
@ -343,15 +340,15 @@ namespace NadekoBot.Modules
await client.CurrentUser.Edit(NadekoBot.password, e.GetArg("new_name"));
});
cgb.CreateCommand(".setgame")
.Description("Sets the bots game.")
.Parameter("set_game", ParameterType.Unparsed)
.Do(e => {
if (e.User.Id != NadekoBot.OwnerID || e.GetArg("set_game") == null) return;
client.SetGame(e.GetArg("set_game"));
});
cgb.CreateCommand(".setgame")
.Description("Sets the bots game.")
.Parameter("set_game", ParameterType.Unparsed)
.Do(e => {
if (e.User.Id != NadekoBot.OwnerID || e.GetArg("set_game") == null) return;
client.SetGame(e.GetArg("set_game"));
});
cgb.CreateCommand(".checkmyperms")
.Description("Checks your userspecific permissions on this channel.")
@ -363,7 +360,7 @@ namespace NadekoBot.Modules
output += "```";
await e.User.SendMessage(output);
});
Server commsServer = null;
User commsUser = null;
@ -393,7 +390,7 @@ namespace NadekoBot.Modules
cgb.CreateCommand(".send")
.Description("Send a message to someone on a different server through the bot.**Owner only.**\n **Usage**: .send Message text multi word!")
.Parameter("msg",ParameterType.Unparsed)
.Parameter("msg", ParameterType.Unparsed)
.Do(async e => {
if (e.User.Id != NadekoBot.OwnerID) return;
try {
@ -435,34 +432,5 @@ namespace NadekoBot.Modules
*/
});
}
bool announcingGreet = false;
Channel announceChannel = null;
Server joinServer = null;
string announceMsg = "Welcome to the server %user%";
private void Client_UserJoined(object sender, UserEventArgs e) {
if (e.Server != joinServer) return;
try {
announceChannel?.Send(announceMsg.Replace("%user%", e.User.Mention));
} catch (Exception) {
Console.WriteLine("Failed sending greet message to the specified channel");
}
}
bool announcingLeave = false;
Channel announceLeaveChannel = null;
Server leaveServer = null;
string announceLeaveMsg = "%user% has left the server";
private void Client_UserLeft(object sender, UserEventArgs e) {
if (e.Server != leaveServer) return;
try {
announceLeaveChannel?.Send(announceLeaveMsg.Replace("%user%", e.User.Mention));
} catch (Exception) {
Console.WriteLine("Failed sending leave message to the specified channel.");
}
}
}
}

View File

@ -182,7 +182,13 @@ namespace NadekoBot.Modules
.Parameter("mention", ParameterType.Required)
.Do(async e =>
{
List<string> praises = new List<string> { " You are cool.", " You are nice!", " You did a good job.", " You did something nice.", " is awesome!" };
List<string> praises = new List<string> { " You are cool.",
" You are nice!",
" You did a good job.",
" You did something nice.",
" is awesome!",
" Wow."};
Random r = new Random();
var u = e.Channel.FindUsers(e.GetArg("mention")).FirstOrDefault();
@ -320,21 +326,26 @@ namespace NadekoBot.Modules
.Description("Shows the message where you were last mentioned in this channel (checks last 10k messages)")
.Do(async e => {
Message msg;
Message msg = null;
var msgs = e.Channel.Messages
.Where(m => m.MentionedUsers.Contains(e.User))
.OrderByDescending(m => m.Timestamp);
if (msgs.Count() > 0)
msg = msgs.FirstOrDefault();
msg = msgs.First();
else {
var msgsarr = await e.Channel.DownloadMessages(10000);
msg = msgsarr
.Where(m => m.MentionedUsers.Contains(e.User))
.OrderByDescending(m => m.Timestamp)
.FirstOrDefault();
int attempt = 0;
Message lastMessage = null;
while (msg == null && attempt++ < 5) {
var msgsarr = await e.Channel.DownloadMessages(100, lastMessage?.Id);
msg = msgsarr
.Where(m => m.MentionedUsers.Contains(e.User))
.OrderByDescending(m => m.Timestamp)
.FirstOrDefault();
lastMessage = msgsarr.OrderBy(m => m.Timestamp).First();
}
}
if (msg != null)
await e.Send("Last message mentioning you was at " + msg.Timestamp + "\n**Message:** " + msg.RawText.Replace("@everyone","@everryone"));
await e.Send($"Last message mentioning you was at {msg.Timestamp}\n**Message from {msg.User.Name}:** {msg.RawText.Replace("@everyone", "@everryone")}");
else
await e.Send("I can't find a message mentioning you.");
});

View File

@ -58,9 +58,9 @@ namespace NadekoBot.Modules {
.Do(e => {
if (musicPlayers.ContainsKey(e.Server) == false) return;
var player = musicPlayers[e.Server];
player.Stop();
MusicControls throwAwayValue;
musicPlayers.TryRemove(e.Server, out throwAwayValue);
player.Stop();
});
cgb.CreateCommand("p")
@ -92,7 +92,7 @@ namespace NadekoBot.Modules {
}
try {
var sr = player.CreateStreamRequest(e, e.GetArg("query"), player.VoiceChannel);
var sr = await player.CreateStreamRequest(e, e.GetArg("query"), player.VoiceChannel);
if (sr == null)
throw new NullReferenceException("StreamRequest is null.");
Message msg = null;