Ejemplo n.º 1
0
        public static async Task Message(MessageCreateEventArgs e)
        {
            var g = Guilds.GetGuild(e.Guild);

            if (g.BridgeChannel == 0 || g.BridgeChannel != e.Channel.Id || e.Message.Author.IsCurrent)
            {
                return;
            }
            foreach (var guild in Guilds.GuildList)
            {
                if (guild.BridgeChannel == 0 || e.Guild.Id == guild.Id)
                {
                    continue;
                }
                var c = await e.Client.GetChannelAsync(guild.BridgeChannel);

                var header = $"**{e.Author.Username}** in `{e.Guild.Name}`: ";
                if (e.Message.Attachments.Count == 0)
                {
                    await c.SendMessageAsync(header + e.Message.Content);
                }
                else
                {
                    var content = e.Message.Content == ""
                        ? $"No Content.\n Attached file: {e.Message.Attachments.First().Url}"
                        : $"{e.Message.Content}\nAttached file: {e.Message.Attachments.First().Url}";
                    await c.SendMessageAsync(header + content);
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task MessageCreated(MessageCreateEventArgs e)
        {
            //Bots don't count
            if (e.Message.Author.IsBot)
            {
                return;
            }
            //Check if guild has the "feature" enabled
            if (Guilds.GetGuild(e.Guild).PrevMessageAmount == 0)
            {
                return;
            }
            //Put current guild the message was received in into variable
            var guild = Guilds.GetGuild(e.Guild);

            //Add channel if it isn't already in the dictionary
            if (!guild.PrevMessages.ContainsKey(e.Channel.Id))
            {
                guild.PrevMessages.TryAdd(e.Channel.Id, new KeyValuePair <DiscordMessage, int>(e.Message, 0));
            }

            var(_, value) = guild.PrevMessages[e.Channel.Id];

            if (guild.PrevMessages[e.Channel.Id].Key == null)
            {
                guild.PrevMessages.TryUpdate(e.Channel.Id, new KeyValuePair <DiscordMessage, int>(e.Message, 1),
                                             guild.PrevMessages[e.Channel.Id]);
                return;
            }

            //Check for the same content
            if (guild.PrevMessages[e.Channel.Id].Key.Content == e.Message.Content &&
                guild.PrevMessages[e.Channel.Id].Key.Author != e.Message.Author)
            {
                //Update the Count
                guild.PrevMessages.TryUpdate(e.Channel.Id, new KeyValuePair <DiscordMessage, int>(e.Message, value + 1),
                                             guild.PrevMessages[e.Channel.Id]);
            }
            else
            {
                //Set count to 0 and update last message
                guild.PrevMessages.TryUpdate(e.Channel.Id, new KeyValuePair <DiscordMessage, int>(e.Message, 1),
                                             guild.PrevMessages[e.Channel.Id]);
            }

            if (guild.PrevMessages[e.Channel.Id].Value >= guild.PrevMessageAmount)
            {
                //Send message and reset count/last message
                await e.Channel.SendMessageAsync(e.Message.Content);

                guild.PrevMessages.TryUpdate(e.Channel.Id, new KeyValuePair <DiscordMessage, int>(null, 1),
                                             guild.PrevMessages[e.Channel.Id]);
            }
        }
Ejemplo n.º 3
0
        public static async Task UserJoined(GuildMemberAddEventArgs e)
        {
            var guild = Guilds.GetGuild(e.Guild);

            //Abort if the guild doesn't have join/leave messages set up
            if (!ShouldSendMessage(guild.JoinMsg, guild.JlMessageChannel))
            {
                return;
            }

            await e.Guild.GetChannel(guild.JlMessageChannel)
            .SendMessageAsync(guild.JoinMsg.Replace("[user]", e.Member.Mention));
        }
Ejemplo n.º 4
0
        public static async Task ReactionRemoved(MessageReactionRemoveEventArgs e)
        {
            var guild = Guilds.GetGuild(e.Message.Channel.Guild);

            if (guild.BoardChannel == 0 || guild.ReactionNeeded == 0)
            {
                return;
            }
            if (e.User.IsBot)
            {
                return;
            }

            var msg = await e.Channel.GetMessageAsync(e.Message.Id);

            //Abort if the message has already been posted to the board

            if (Messages.BoardMessages.Exists(x => x.SourceId == msg.Id && x.Sent))
            {
                var bmsg = await e.Channel.Guild.GetChannel(guild.BoardChannel)
                           .GetMessageAsync(Messages.BoardMessages.Find(x => x.SourceId == msg.Id).BoardId);

                //Loop through all reactions on the message
                var reactions = new List <string>();
                foreach (var reaction in msg.Reactions)
                {
                    if (reaction.Count < guild.ReactionNeeded)
                    {
                        continue;
                    }
                    try
                    {
                        reactions.Add(reaction.Emoji.IsAnimated
                            ? $"{DiscordEmoji.FromGuildEmote(Bot.Client, reaction.Emoji.Id)} x {reaction.Count}"
                            : $"{DiscordEmoji.FromName(Bot.Client, reaction.Emoji.GetDiscordName())} x {reaction.Count}");
                    }
                    catch (Exception)
                    {
                        reactions.Add($"{reaction.Emoji.GetDiscordName()} x {reaction.Count}");
                    }
                }

                await bmsg.ModifyAsync(string.Join(" • ", reactions));
            }
        }
Ejemplo n.º 5
0
        public static async Task UserRemoved(GuildMemberRemoveEventArgs e)
        {
            var guild = Guilds.GetGuild(e.Guild);

            if (guild == null)
            {
                return;
            }

            //Abort if the guild doesn't have join/leave messages set up
            if (!ShouldSendMessage(guild.LeaveMsg, guild.JlMessageChannel))
            {
                return;
            }

            await e.Guild.GetChannel(guild.JlMessageChannel)
            .SendMessageAsync(guild.LeaveMsg.Replace("[user]", $"{e.Member.Username}#{e.Member.Discriminator}"));
        }
Ejemplo n.º 6
0
        public static async Task ReactionAdded(MessageReactionAddEventArgs e)
        {
            //Bot reactions don't count
            if (e.User.IsBot)
            {
                return;
            }
            //Put into variable for convenience
            var guild = Guilds.GetGuild(e.Message.Channel.Guild);

            //Abort if the guild has no board set up
            if (guild.BoardChannel == 0 || guild.ReactionNeeded == 0)
            {
                return;
            }

            //This means that it was the first reaction to the message, and that we have to add it to the Database
            //if (!Messages.BoardMessages.ContainsKey(e.Message.Id))
            if (!Messages.BoardMessages.Exists(x => x.SourceId == e.Message.Id))
            {
                Messages.BoardMessages.Add(new Messages.Message {
                    SourceId = e.Message.Id, BoardId = 0, Sent = false
                });
                Utilities.Con.Open();
                using (var cmd =
                           new SqliteCommand("INSERT INTO Messages (id,sent,bId) VALUES (@id,@sent,'0')",
                                             Utilities.Con))
                {
                    cmd.Parameters.AddWithValue("@sent", false);
                    cmd.Parameters.AddWithValue("@id", e.Message.Id);
                    cmd.ExecuteReader();
                }

                Utilities.Con.Close();
            }

            //Put into variable for convenience
            var msg = await e.Channel.GetMessageAsync(e.Message.Id);

            //Abort if the message has already been posted to the board

            if (Messages.BoardMessages.Exists(x => x.SourceId == msg.Id && x.Sent))
            {
                var bmsg = await e.Channel.Guild.GetChannel(guild.BoardChannel)
                           .GetMessageAsync(Messages.BoardMessages.Find(x => x.SourceId == msg.Id).BoardId);

                //Loop through all reactions on the message
                var reactions = new List <string>();
                foreach (var reaction in msg.Reactions)
                {
                    if (reaction.Count < guild.ReactionNeeded)
                    {
                        continue;
                    }
                    try
                    {
                        reactions.Add(reaction.Emoji.IsAnimated
                            ? $"{DiscordEmoji.FromGuildEmote(Bot.Client, reaction.Emoji.Id)} x {reaction.Count}"
                            : $"{DiscordEmoji.FromName(Bot.Client, reaction.Emoji.GetDiscordName())} x {reaction.Count}");
                    }
                    catch (Exception)
                    {
                        reactions.Add(reaction.Emoji.GetDiscordName());
                    }
                }

                await bmsg.ModifyAsync(string.Join(" • ", reactions));
            }
            else
            {
                var sendIt    = false;
                var reactions = new List <string>();
                //Loop through all reactions on the message
                foreach (var reaction in msg.Reactions)
                {
                    //If the current reaction is less than what we need, we skip this one
                    if (reaction.Count < guild.ReactionNeeded)
                    {
                        continue;
                    }
                    try
                    {
                        reactions.Add(reaction.Emoji.IsAnimated
                            ? $"{DiscordEmoji.FromGuildEmote(Bot.Client, reaction.Emoji.Id)} x {reaction.Count}"
                            : $"{DiscordEmoji.FromName(Bot.Client, reaction.Emoji.GetDiscordName())} x {reaction.Count}");
                    }
                    catch (Exception)
                    {
                        reactions.Add($"{reaction.Emoji.GetDiscordName()} x {reaction.Count}");
                    }

                    sendIt = true;
                }

                if (sendIt)
                {
                    //Post the message to the board
                    var bmsg = await e.Message.Channel.Guild.GetChannel(guild.BoardChannel)
                               .SendMessageAsync(string.Join(" • ", reactions), false, BuildEmbed(msg));

                    //Update it in our local collection and in the database so it doesn't get sent twice
                    Messages.BoardMessages.Remove(Messages.BoardMessages.Find(x => x.SourceId == e.Message.Id));
                    Messages.BoardMessages.Add(new Messages.Message
                    {
                        SourceId = msg.Id,
                        BoardId  = bmsg.Id,
                        Sent     = true
                    });
                    Utilities.Con.Open();
                    using (var cmd =
                               new SqliteCommand(
                                   "UPDATE Messages SET sent= 'true', bId = @bid WHERE Messages.id = @id",
                                   Utilities.Con))
                    {
                        cmd.Parameters.AddWithValue("@bid", bmsg.Id);
                        cmd.Parameters.AddWithValue("@id", e.Message.Id);
                        cmd.ExecuteReader();
                    }

                    Utilities.Con.Close();
                }
            }
        }