コード例 #1
0
ファイル: Program.cs プロジェクト: silasary/zigzagoon
        private static Action IdleFunc(Slack slack)
        {
            return(() =>
            {
                try
                {
                    Thread.CurrentThread.Name = slack.TeamInfo.Domain + "_idlefunc";
                    Thread.CurrentThread.IsBackground = true;
                    if (!PersistentSingleton <Settings> .Instance.HasDoneIntroSpiel)
                    {
                        Thread.Sleep(TimeSpan.FromMinutes(1));
                    }
                    DateTime LastQuestion = new DateTime();
                    int askchannel = random.Next(0, slack.JoinedChannels.Count());
                    while (true)
                    {
                        Singleton <NetworkData> .Instance.Refresh();

#if REMINDERS
                        try
                        {
                            PersistentSingleton <Reminders> .Instance.Check(slack);
                        }
                        catch (Exception)
                        { }
#endif
                        if (DateTime.Now.Hour < 7)
                        {
                            continue;
                        }
                        var inc = Singleton <NetworkData> .Instance.PickIncompleteHost();

                        if (inc != null && LastQuestion.Date != DateTime.Now.Date)
                        {
                            LastQuestion = DateTime.Now;
                            string chan = slack.JoinedChannels.ElementAt(askchannel).Id;
                            slack.SendMessage(chan, "Excuse me, but does anyone recognise '{0}'?", inc.FriendlyName);
                            askchannel++;
                            if (askchannel == slack.JoinedChannels.Count())
                            {
                                askchannel = 0;
                            }
                            slack.SetLastHost(inc);
                        }
                        Thread.Sleep(new TimeSpan(0, 5, 0));
                    }
                }
                catch (Exception c)
                {
                    Console.WriteLine("!An Error has been caught!\n{0}", c);
                    File.WriteAllText("Idlefunc_error.txt", c.ToString());
                }
            });
        }
コード例 #2
0
        public bool Run(string MessageText, Message RawMessage, bool IsTargeted, Slack Instance)
        {
            var match = Regex.Match(
                MessageText,
                @"(Call|Name|Nickname) it (?<Nickname>[\w _:]+)",
                RegexOptions.IgnoreCase);

            if (match.Success)
            {
                if (!IsTargeted)
                {
                    //TODO: Rate Limit to once per day.
                    RawMessage.Reply(Instance, "Was that to me?");
                    return(true);
                }
                var LastHost = Instance.GetLastHost();
                if (LastHost == null)
                {
                    RawMessage.Reply(Instance, "I have no idea what you're talking about.", true);
                    return(true);
                }
                var oldname = LastHost.FriendlyName;
                LastHost.Name = match.Groups["Nickname"].Value;
                RawMessage.Reply(Instance, string.Format("Ok. It'll call {0} \"{1}\" from now on.", oldname, LastHost.FriendlyName));
                return(true);
            }
            match = Regex.Match(
                MessageText,
                @"(Call|Name|Nickname) (?<name>[\w:]+) (as|to) (?<Nickname>[\w _:]+)",
                RegexOptions.IgnoreCase);
            if (match.Success)
            {
                if (!IsTargeted)
                {
                    //TODO: Rate Limit to once per day.
                    RawMessage.Reply(Instance, "Was that to me?");
                    return(true);
                }
                Host        LastHost;
                NetworkData network = Singleton <NetworkData> .Instance;
                Instance.SetLastHost(LastHost = network.Find(match.Groups["name"].Value));
                if (LastHost == null)
                {
                    Instance.SendMessage(RawMessage.Channel, "I couldn't find anything by that name");
                    return(true);
                }
                var oldname = LastHost.FriendlyName;
                LastHost.Name = match.Groups["Nickname"].Value;
                RawMessage.Reply(Instance, string.Format("Ok. It'll call {0} \"{1}\" from now on.", oldname, LastHost.FriendlyName));
                return(true);
            }
            return(false);
        }
コード例 #3
0
ファイル: WhosInCommand.cs プロジェクト: silasary/zigzagoon
        bool ICommand.Run(string MessageText, Message RawMessage, bool IsTargeted, Slack Instance)
        {
            // ’
            var match = Regex.Match(
                MessageText,
                @"(Who['’]?s|(Any|Some)(one|body)) in\?",
                RegexOptions.IgnoreCase);

            if (match.Success)
            {
                Singleton <NetworkData> .Instance.Refresh();

                var hosts = Singleton <NetworkData> .Instance.CertainHosts().ToList();

                var unknowns = Singleton <NetworkData> .Instance.UnknownHosts().Count();

                var  people = new List <string>();
                Host h      = null;
                foreach (var host in hosts.ToArray())
                {
                    if (people.Contains(host.Owner))
                    {
                        hosts.Remove(host);
                    }
                    else
                    {
                        people.Add(host.Owner);
                    }
                }

                if (hosts.Count > 0)
                {
                    Instance.SendMessage(RawMessage.Channel, string.Format("{0} {1} here. {2}",
                                                                           string.Join(", ", hosts.Select(host => String.Format("{0}'s {1} '{2}'", host.Owner, host.Type, host.FriendlyName))),
                                                                           hosts.Count == 1 ? "is" : "are",
                                                                           unknowns > 0 ? string.Format("There are also {0} unknown devices.", unknowns) : ""));
                }
                else if ((h = Singleton <NetworkData> .Instance.PickIncompleteHost()) != null)
                {
                    Instance.SendMessage(RawMessage.Channel, String.Format("I don't know. But there is a device I don't recognise: {0}", h.FriendlyName));
                }
                else
                {
                    Instance.SendMessage(RawMessage.Channel, "Nobody. It's lonely here :frowning:");
                }
                Instance.SetLastHost(h);
                return(true);
            }
            return(false);
        }