Exemple #1
0
 public void Delete(BotMessage msg)
 {
     if (msg != null)
     {
         _client.DeleteMessageAsync((long)msg.channelID, (int)msg.msgID);
     }
 }
Exemple #2
0
        public void ListCommands(BotMessage msg, string prefix = null)
        {
            var platform = _connections[msg.Sender.Platform];

            var sb = new StringBuilder();

            foreach (var entry in _commands.Values)
            {
                if (entry.name == "help")
                {
                    continue;
                }

                var visible = entry.filter != null?entry.filter(msg) : true;

                if (visible)
                {
                    sb.AppendLine($"{platform.GetCommandPrefix()}{entry.name}\t\t{entry.description}");
                }
            }

            if (sb.Length > 0)
            {
                var output = sb.ToString();
                if (!string.IsNullOrEmpty(prefix))
                {
                    output = prefix + output;
                }
                Speak(msg.Sender.Platform, msg.Sender.ID, output);
            }
        }
Exemple #3
0
        private int ShowWhoIs(BotMessage msg, int state)
        {
            switch (state)
            {
            case 1:
                long ID;

                if (long.TryParse(msg.Text, out ID))
                {
                    try {
                        var user = Expand(msg.Sender.Platform, ID);
                        Speak(msg.Sender, $"ID {ID} belongs to user with handle @{user.Handle}");
                    }
                    catch
                    {
                        Speak(msg.Sender, $"Could not find user with ID {ID}");
                    }
                }
                else
                {
                    Speak(msg.Sender, "That's not an valid ID!");
                }
                return(0);

            default:
                Speak(msg.Sender, "What ID you want to look up?");
                return(1);
            }
        }
Exemple #4
0
        public void Delete(BotMessage msg)
        {
            if (msg != null)
            {
                var id     = (ulong)msg.channelID;
                var socket = GetChannel(id);

                socket.DeleteMessagesAsync(new ulong[] { (ulong)msg.msgID });
            }
        }
Exemple #5
0
        private int PromoteAdmin(BotMessage msg, int state)
        {
            switch (state)
            {
            case 1:
            {
                if (msg.Kind == MessageKind.Text)
                {
                    long targetID;

                    if (long.TryParse(msg.Text, out targetID))
                    {
                        var target = new MessageSender()
                        {
                            Platform = msg.Sender.Platform, ID = targetID
                        };

                        if (Admins.Contains(target))
                        {
                            Speak(msg.Sender.Platform, msg.Sender.ID, $"This user already is an admin.");
                            return(0);
                        }

                        AddAdmin(target);
                        Speak(msg.Sender, $"That user is now an admin!");
                    }
                    else
                    {
                        Speak(msg.Sender, $"You should give me an {msg.Sender.Platform} user ID please...");
                    }
                }
                else
                {
                    Speak(msg.Sender, $"You should give me an {msg.Sender.Platform} user ID please...");
                }

                return(0);
            }

            default:
            {
                Speak(msg.Sender, $"Who should be promoted to admin?");
                return(1);
            }
            }

            foreach (var entry in _groupList)
            {
                Speak(msg.Sender.Platform, msg.Sender.ID, $"{entry.Value} => {entry.Key}");
            }

            return(0);
        }
Exemple #6
0
        private int WhereCommand(BotMessage msg, int state)
        {
            if (_groupList.Count == 0)
            {
                Speak(msg.Sender.Platform, msg.Sender.ID, $"No groups found");
                return(0);
            }

            foreach (var entry in _groupList)
            {
                Speak(msg.Sender.Platform, msg.Sender.ID, $"{entry.Value} => {entry.Key}");
            }

            return(0);
        }
Exemple #7
0
        private async Task MessageReceived(SocketMessage src)
        {
            try
            {
                if (src.Author.Id == _client.CurrentUser.Id)
                {
                    return;
                }

                MessageKind kind;

                var msg = new BotMessage();
                msg.Visibility = (src.Channel is IPrivateChannel) ? MessageVisibility.Private : MessageVisibility.Public;


                var sourceID     = msg.Visibility == MessageVisibility.Private ? src.Author.Id : src.Channel.Id;
                var sourceHandle = msg.Visibility == MessageVisibility.Private ? src.Author.Username : src.Channel.Name;
                var sourceName   = msg.Visibility == MessageVisibility.Private ? src.Author.Username : src.Channel.Name;

                msg.Kind   = MessageKind.Text;
                msg.Text   = src.Content;
                msg.Sender = new MessageSender()
                {
                    ID = (long)sourceID, Handle = sourceHandle, Name = sourceName, Platform = BotPlatform.Discord
                };
                msg.File = null;

                msg.channelID = src.Channel.Id;
                msg.msgID     = (ulong)src.Id;
                msg.platform  = BotPlatform.Discord;

                _channels[sourceID] = src.Channel;

                _queue.Enqueue(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemple #8
0
        private void OnMessage(object sender, MessageEventArgs e)
        {
            var src = e.Message;

            try
            {
                MessageKind kind;
                switch (src.Type)
                {
                case MessageType.Text: kind = MessageKind.Text; break;

                case MessageType.Document: kind = MessageKind.File; break;

                case MessageType.Photo: kind = MessageKind.File; break;

                case MessageType.ChatMembersAdded: kind = MessageKind.Join; break;

                default: kind = MessageKind.Other; break;
                }

                var msg = new BotMessage();
                msg.Visibility = src.Chat.Type == ChatType.Private ? MessageVisibility.Private : MessageVisibility.Public;
                msg.Kind       = kind;
                msg.Sender     = FromChat(src.Chat);

                string      text = "";
                MessageFile file = null;

                switch (src.Type)
                {
                case MessageType.ChatMembersAdded:
                {
                    if (src.NewChatMembers != null)
                    {
                        foreach (var member in src.NewChatMembers)
                        {
                            if (text.Length > 0)
                            {
                                text += ", ";
                            }

                            text += Combine(member.FirstName, member.LastName);
                        }
                    }

                    text = "Joined: " + text;
                    return;
                }

                case MessageType.Photo:
                {
                    file = new MessageFile();
                    var i      = src.Photo.Length - 1;    //to get the highest resolution version of the photo
                    var fileId = src.Photo[i].FileId;

                    var info = _client.GetFileAsync(fileId).GetAwaiter().GetResult();

                    file.Size  = src.Photo[i].FileSize;
                    file.Name  = Path.GetFileName(info.FilePath);
                    file.Fetch = () => FetchFile(fileId);
                    break;
                }

                case MessageType.Document:
                {
                    file = new MessageFile();
                    var fileId = src.Document.FileId;
                    file.Size  = src.Document.FileSize;
                    file.Name  = src.Document.FileName;
                    file.Fetch = () => FetchFile(fileId);
                    break;
                }

                case MessageType.Text:
                {
                    text = src.Text;
                    break;
                }

                default: file = null; break;
                }

                msg.File      = file;
                msg.Text      = text;
                msg.channelID = src.Chat.Id;
                msg.msgID     = src.MessageId;
                msg.platform  = BotPlatform.Telegram;

                _queue.Enqueue(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemple #9
0
        public void Delete(BotMessage msg)
        {
            var connection = _connections[msg.platform];

            connection.Delete(msg);
        }
Exemple #10
0
 protected virtual void OnMessage(BotMessage msg)
 {
 }
Exemple #11
0
        private void ProcessMessage(BotMessage msg)
        {
            var text = msg.Text;

            string queue = null;

            if (msg.Visibility == MessageVisibility.Public && !_groupList.ContainsKey(msg.Sender.ID))
            {
                var content = $"{msg.Sender.Platform} => {msg.Sender.ID} / {msg.Sender.Handle}";
                Console.WriteLine("Listening in group: " + content);
                _groupList[msg.Sender.ID] = content;
            }

            BotCommand cmd    = null;
            var        prefix = _connections[msg.Sender.Platform].GetCommandPrefix();

            if (_cmds.ContainsKey(msg.Sender.ID))
            {
                cmd = _cmds[msg.Sender.ID];
            }
            else
            if (text.StartsWith(prefix))
            {
                var temp = text.Substring(prefix.Length).Split(new char[] { ' ' }, 2);

                var key = temp[0];

                if (_commands.ContainsKey(key))
                {
                    cmd = _commands[key];

                    if (temp.Length == 2 && cmd.AllowShortcut)
                    {
                        queue = temp[1];
                    }
                }
            }

            if (cmd != null)
            {
                var state = _state.ContainsKey(msg.Sender.ID) ? _state[msg.Sender.ID] : 0;
                if (state != 0 || (cmd.filter == null || cmd.filter(msg)))
                {
                    if (queue != null && state == 0)
                    {
                        msg.Text = queue;
                        state    = 1;
                    }

                    state = cmd.handler(msg, state);
                    if (state > 0)
                    {
                        _state[msg.Sender.ID] = state;
                        _cmds[msg.Sender.ID]  = cmd;
                    }
                    else
                    {
                        try
                        {
                            _state.Remove(msg.Sender.ID);
                            _cmds.Remove(msg.Sender.ID);
                        }
                        catch
                        {
                            // ignore
                        }
                    }
                }
                else
                {
                    OnPermissionFailedForCommand(msg);
                }

                return;
            }

            OnMessage(msg);

            // update last seen
            var lastSeen = GetLastSeen(msg.Sender);
            var diff     = (DateTime.UtcNow - lastSeen).TotalHours;

            if (diff > 1)
            {
                var timestamp = DateTime.UtcNow.Ticks;
                Times.Set(msg.Sender, timestamp.ToString());
            }
        }
Exemple #12
0
 protected virtual void OnPermissionFailedForCommand(BotMessage msg)
 {
 }
Exemple #13
0
 protected virtual BotMessage FilterMessage(BotMessage msg)
 {
     return(msg);
 }
Exemple #14
0
 private int ShowMe(BotMessage msg, int state)
 {
     Speak(msg.Sender, $"Your {msg.Sender.Platform} ID is {msg.Sender.ID}");
     return(0);
 }
Exemple #15
0
 public bool IsCommand(BotMessage msg, string cmd)
 {
     return(msg.Text.StartsWith(_connections[msg.Sender.Platform].GetCommandPrefix() + cmd));
 }