public static void AddStrike(ulong user, DateTime time, TimeSpan span, string strikeReason) { if (strikes.ContainsKey(user)) { strikes [user].strikeTime.Add(span); strikes [user].reason += ", " + strikeReason; } else { Strike newStrike = new Strike() { reason = strikeReason, strikeDate = time, strikeTime = span }; strikes.Add(user, newStrike); // Well these names are not confusing. I suppose this is why we make constructors. } SocketGuildUser socketUser = Utility.GetServer().GetUser(user); SocketRole role = GetRole(); if (!socketUser.Roles.Contains(role)) { Utility.SecureAddRole(socketUser, role); } Program.messageControl.SendMessage(socketUser, strikeGivenMessage.Replace("{STRIKEREASON}", strikeReason).Replace("{STRIKERAISEDATE}", strikes[user].strikeDate.Add(strikes[user].strikeTime).ToString())); AutoCommands.RunEvent(AutoCommands.Event.UserStricken, user.ToString(), time.ToString(), strikeReason); Save(); }
public Task OnSecondPassed(DateTime now) { List <Event> toRemoveUpcoming = new List <Event> (); List <Event> toRemoveOngoing = new List <Event> (); toAdd = new List <Event> (); foreach (Event e in upcomingEvents) { if (e.eventState == Event.EventState.Awaiting) { if (now > e.time) { StartEvent(e); } } } foreach (Event e in ongoingEvents) { if (e.time.Add(e.duration) < DateTime.Now && e.eventState != Event.EventState.Ended) { e.eventState = Event.EventState.Ended; AutoCommands.RunEvent(AutoCommands.Event.EventEnded, e.name); } } toRemoveUpcoming.AddRange(upcomingEvents.Where(x => x.eventState != Event.EventState.Awaiting)); toRemoveOngoing.AddRange(ongoingEvents.Where(x => x.eventState == Event.EventState.Ended)); bool doSave = toRemoveOngoing.Count + toRemoveUpcoming.Count + toAdd.Count > 0 ? true : false; foreach (Event r in toRemoveUpcoming) { upcomingEvents.Remove(r); } foreach (Event r in toRemoveOngoing) { ongoingEvents.Remove(r); } foreach (Event a in toAdd) { upcomingEvents.Add(a); } if (doSave) { SaveEvents(); } return(Task.CompletedTask); }
public static void RaiseStrike(ulong user) { if (strikes.ContainsKey(user)) { SocketRole role = GetRole(); SocketGuildUser socketUser = Utility.GetServer().GetUser(user); Utility.SecureRemoveRole(socketUser, role); strikes.Remove(user); Program.messageControl.SendMessage(socketUser, strikeRaisedMessage); AutoCommands.RunEvent(AutoCommands.Event.UserStricken, user.ToString()); } Save(); }
public static void StartEvent(Event startingEvent) { Logging.Log(Logging.LogType.BOT, "Starting event: " + startingEvent.name + "!"); startingEvent.eventState = Event.EventState.InProgress; AutoCommands.RunEvent(AutoCommands.Event.EventStarted, startingEvent.name); if (startingEvent.eventMemberIDs.Count != 0) { string mentions = ""; foreach (ulong id in startingEvent.eventMemberIDs) { SocketGuildUser user = Utility.GetServer().GetUser(id); if (user != null) { mentions += ", " + user.Mention; } } if (startingEvent.eventMemberIDs.Count > 0) { string members; Embed eventEmbed = ConstructEmbed(startingEvent, out members); Program.messageControl.SendEmbed(Utility.GetMainChannel() as SocketTextChannel, eventEmbed, members); Voice.CreateTemporaryChannel(startingEvent.name, startingEvent.duration); ongoingEvents.Add(startingEvent); } else { try { Program.messageControl.SendMessage( Utility.GetMainChannel() as SocketTextChannel, "Event **" + startingEvent.name + "** cancelled, since no one showed up. :(", true); startingEvent.eventState = Event.EventState.Ended; }catch (Exception e) { Logging.Log(e); } } if (startingEvent.repeatTime.Ticks != 0) { Event evt = CreateAndReturnEvent(startingEvent.name, startingEvent.time.Add(startingEvent.repeatTime), startingEvent.duration, startingEvent.hostID, startingEvent.iconUrl, startingEvent.description, startingEvent.repeatTime); evt.eventMemberIDs = startingEvent.eventMemberIDs; toAdd.Add(evt); } } }
private void AnnounceBirthday(Date date) { SocketGuildUser user = Utility.GetServer().GetUser(date.userID); SocketGuildChannel main = Utility.GetMainChannel(); AutoCommands.RunEvent(AutoCommands.Event.UserBirthday, user.Id.ToString()); // I have no idea if this works, and it's possibly the worst possible way I could have done that. int age = 0; try { age = DateTime.MinValue.Add(DateTime.Now - new DateTime(date.day.Year, date.day.Month, date.day.Day)).Year - DateTime.MinValue.Year; } catch (IndexOutOfRangeException) { Logging.Log(Logging.LogType.EXCEPTION, user.Username + " has somehow set their birthday to be before now. wat."); } string ageSuffix = "'th"; switch (age.ToString().LastOrDefault()) { case '1': ageSuffix = "'st"; break; case '2': ageSuffix = "'nd"; break; case '3': ageSuffix = "'rd"; break; } if (age % 10 > 0 && age % 10 < 4) { ageSuffix = "'th"; } Program.messageControl.SendMessage(main as SocketTextChannel, onBirthdayAnnouncementMessage.Replace("{USERNAME}", Utility.GetUserName(user)).Replace("{AGE}", age + ageSuffix), true); Program.messageControl.SendMessage(user, onBirthdayCongratulationsDM); }
public void Initialize() { DateTime now = DateTime.Now; for (int i = 0; i < clockables.Length; i++) { if (clockablesEnabled[i]) { clockables[i].Initialize(now); } } while (timeThread.IsAlive) { now = DateTime.Now.AddHours(-offsetHours); // This could possibly be improved with delegates, but I have no idea how they work. // Don't do anything before the server is ready. if (Program.FullyBooted()) { if (now.Second != lastMesauredTime.Second) { for (int i = 0; i < clockables.Length; i++) { if (clockablesEnabled [i]) { clockables [i].OnSecondPassed(now); } } } if (now.Minute != lastMesauredTime.Minute) { for (int i = 0; i < clockables.Length; i++) { if (clockablesEnabled [i]) { clockables [i].OnMinutePassed(now); } } } if (now.Hour != lastMesauredTime.Hour) { for (int i = 0; i < clockables.Length; i++) { if (clockablesEnabled [i]) { clockables [i].OnHourPassed(now); } } AutoCommands.RunEvent(AutoCommands.Event.HourPassed, now.ToString()); } if (now.Day != lastMesauredTime.Day) { for (int i = 0; i < clockables.Length; i++) { if (clockablesEnabled [i]) { clockables [i].OnDayPassed(now); } } AutoCommands.RunEvent(AutoCommands.Event.DayPassed, now.ToString()); } } Thread.Sleep(checkDelay); lastMesauredTime = now; } }
public async Task Start(string [] args) { dataPath = AppContext.BaseDirectory + "/data/"; dataPath = dataPath.Replace('\\', '/'); InitializeDirectories(); Logging.Log(Logging.LogType.BOT, "Initializing bot.. Datapath: " + dataPath); BotConfiguration.Initialize(); Encryption.Initialize(); BotConfiguration.AddConfigurable(this); LoadConfiguration(); discordClient = new DiscordSocketClient(); messageControl = new MessageControl(); karma = new Karma(); new StreamingMonitor(); LegalJunk.Initialize(); Logging.Log(Logging.LogType.BOT, "Loading data.."); InitializeCommands(); UserConfiguration.Initialize(); InviteHandler.Initialize(); CommandChain.Initialize(); Permissions.Initialize(); AutoCommands.Initialize(); clock = new Clock(); InitializeData(); UserGameMonitor.Initialize(); bootedTime = DateTime.Now.AddSeconds(BOOT_WAIT_TIME); Logging.Log(Logging.LogType.BOT, "Setting up events.."); discordClient.MessageReceived += async(e) => { Logging.Log(Logging.LogType.CHAT, Utility.GetChannelName(e) + " says: " + e.Content); bool hideTrigger = false; if (e.Content.Length > 0 && ContainsCommandTrigger(e.Content, out hideTrigger)) { FindAndExecuteCommand(e, e.Content, commands, 0, true, true); } if (e.Author.Id != discordClient.CurrentUser.Id) { FindPhraseAndRespond(e); AutoCommands.RunEvent(AutoCommands.Event.MessageRecieved, e.Content, e.Channel.Id.ToString()); } if (e.Content.Length > 0 && hideTrigger) { e.DeleteAsync(); allowedDeletedMessages.Add(e.Content); } }; discordClient.MessageUpdated += async(cache, message, channel) => { Logging.Log(Logging.LogType.CHAT, "Message edited: " + Utility.GetChannelName(message as SocketMessage) + " " + message.Content); AutoCommands.RunEvent(AutoCommands.Event.MessageDeleted, message.Content, message.Channel.Id.ToString()); }; discordClient.MessageDeleted += async(cache, channel) => { IMessage message = await cache.GetOrDownloadAsync(); Logging.Log(Logging.LogType.CHAT, "Message deleted: " + Utility.GetChannelName(channel as SocketGuildChannel)); AutoCommands.RunEvent(AutoCommands.Event.MessageDeleted, channel.Id.ToString()); }; discordClient.UserJoined += async(e) => { Younglings.OnUserJoined(e); RestInviteMetadata possibleInvite = await InviteHandler.FindInviter(); Logging.Log(Logging.LogType.BOT, "User " + e.Username + " joined."); AutoCommands.RunEvent(AutoCommands.Event.UserJoined, e.Id.ToString()); SocketGuildUser inviter; if (possibleInvite != null) { inviter = Utility.GetServer().GetUser(possibleInvite.Inviter.Id); string joinMessage = Utility.SelectRandom(onUserJoinFromInviteMessage); messageControl.SendMessage(Utility.GetMainChannel() as SocketTextChannel, joinMessage.Replace("{USERNAME}", Utility.GetUserName(e)).Replace("{INVITERNAME}", Utility.GetUserName(inviter)), true); } else { string joinMessage = Utility.SelectRandom(onUserJoinMessage); messageControl.SendMessage(Utility.GetMainChannel() as SocketTextChannel, joinMessage.Replace("{USERNAME}", Utility.GetUserName(e)), true); } string [] welcomeMessage = SerializationIO.LoadTextFile(dataPath + "welcomemessage" + gitHubIgnoreType); string combined = ""; for (int i = 0; i < welcomeMessage.Length; i++) { combined += welcomeMessage [i] + "\n"; } await messageControl.SendMessage(e, combined); }; discordClient.UserLeft += (e) => { string leftMessage = Utility.SelectRandom(onUserLeaveMessage); Logging.Log(Logging.LogType.BOT, "User " + e.Username + " left."); AutoCommands.RunEvent(AutoCommands.Event.UserLeft, e.Id.ToString()); if (automaticLeftReason.ContainsKey(e.Id)) { leftMessage = $"**{Utility.GetUserName (e)}** left - " + automaticLeftReason [e.Id]; automaticLeftReason.Remove(e.Id); } messageControl.SendMessage(Utility.GetMainChannel() as SocketTextChannel, leftMessage.Replace("{USERNAME}", Utility.GetUserName(e)), true); return(Task.CompletedTask); }; discordClient.UserVoiceStateUpdated += async(user, before, after) => { Logging.Log(Logging.LogType.BOT, "User voice updated: " + user.Username); SocketGuild guild = (user as SocketGuildUser).Guild; if (after.VoiceChannel != null) { Voice.allVoiceChannels [after.VoiceChannel.Id].OnUserJoined(user as SocketGuildUser); } if (before.VoiceChannel == null && after.VoiceChannel != null) { AutoCommands.RunEvent(AutoCommands.Event.JoinedVoice, user.Id.ToString(), after.VoiceChannel.Id.ToString()); } if (before.VoiceChannel != null && after.VoiceChannel == null) { AutoCommands.RunEvent(AutoCommands.Event.LeftVoice, user.Id.ToString(), before.VoiceChannel.Id.ToString()); } await Voice.OnUserUpdated(guild, before.VoiceChannel, after.VoiceChannel); return; }; discordClient.GuildMemberUpdated += async(before, after) => { if ((before as SocketGuildUser).Nickname != (after as SocketGuildUser).Nickname) { MentionNameChange(before, after); } }; discordClient.UserUpdated += (before, after) => { if (before.Username != after.Username && (after as SocketGuildUser).Nickname == "") { MentionNameChange(before as SocketGuildUser, after as SocketGuildUser); } return(Task.CompletedTask); }; discordClient.UserBanned += (e, guild) => { SocketChannel channel = Utility.GetMainChannel(); if (channel == null) { return(Task.CompletedTask); } string banMessage = Utility.SelectRandom(onUserBannedMessage); messageControl.SendMessage(channel as SocketTextChannel, banMessage.Replace("{USERNAME}", Utility.GetUserName(e as SocketGuildUser)), true); return(Task.CompletedTask); }; discordClient.UserUnbanned += (e, guild) => { SocketChannel channel = Utility.GetMainChannel(); if (channel == null) { return(Task.CompletedTask); } string unbannedMessage = Utility.SelectRandom(onUserUnbannedMessage); messageControl.SendMessage(channel as SocketTextChannel, unbannedMessage.Replace("{USERNAME}", Utility.GetUserName(e as SocketGuildUser)), true); return(Task.CompletedTask); }; discordClient.Ready += () => { Logging.Log(Logging.LogType.BOT, "Bot is ready and running!"); return(Task.CompletedTask); }; string token = ""; try { token = SerializationIO.LoadTextFile(dataPath + "bottoken" + gitHubIgnoreType)[0]; }catch (Exception e) { Logging.Log(Logging.LogType.CRITICAL, "Bottoken not found, please create a file at <botroot>/data/bottoken.botproperty and insert bottoken there. " + e.Message); } Logging.Log(Logging.LogType.BOT, "Connecting to Discord.."); await discordClient.LoginAsync(TokenType.Bot, token); await discordClient.StartAsync(); BotConfiguration.PostInit(); await Utility.AwaitFullBoot(); try { if (args.Length > 0 && args [0] == "true" && onPatchedAnnounceChannel != 0) { using (HttpClient client = new HttpClient()) { string changelog = await client.GetStringAsync(AutoPatcher.url + "changelog.txt"); string version = await client.GetStringAsync(AutoPatcher.url + "version.txt"); string total = $"Succesfully installed new patch, changelog for {version}:\n{changelog}"; SocketGuildChannel patchNotesChannel = Utility.GetServer().GetChannel(onPatchedAnnounceChannel); if (patchNotesChannel != null) { messageControl.SendMessage(patchNotesChannel as ISocketMessageChannel, total, true, "```"); } } } } catch (Exception e) { Logging.Log(e); } BakeQuickCommands(); await Task.Delay(-1); }