Ejemplo n.º 1
0
        private async Task ProcessTextChannels(SocketGuild guild)
        {
            using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
            {
                foreach (SocketTextChannel textChannel in guild.TextChannels)
                {
                    DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                    var channelObj    = new DiscordTextChannel(textChannel.Id.ToString(), guild.Id.ToString(), textChannel.Name, textChannel.Topic, textChannel.CreatedAt, ((textChannel as ISocketPrivateChannel) != null));
                    var channelRecord = await DBContext.TextChannels.FindAsync(channelObj.Id);

                    if (channelRecord == null)
                    {
                        ConsoleEx.WriteColoredLine(LogSeverity.Verbose, ConsoleTextFormat.TimeAndText, "Adding new Text Channel ", ConsoleColor.Cyan, textChannel.Name, ConsoleColor.Gray, " to the database.");
                        await DBContext.AddAsync(channelObj);
                    }
                    else
                    {
                        DBContext.DetachLocal(channelObj, channelObj.Id);
                    }

                    await DBContext.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 2
0
        private void GuildTracker()
        {
            Client.JoinedGuild += async(guild) =>
            {
                using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                {
                    DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                    var existingGuild = DBContext.Guilds.FromSql("SELECT * FROM Guilds WHERE Id = {0} LIMIT 1", guild.Id.ToString()).AsNoTracking().FirstOrDefault();
                    var newGuild      = new DiscordGuild(guild.Id, guild.Name, guild.OwnerId, guild.CreatedAt, guild.IconUrl, guild.SplashUrl);
                    if (existingGuild == null)
                    {
                        await DBContext.AddAsync(newGuild);

                        ConsoleEx.WriteColoredLine($"Joined new Guild $[[DarkCyan]]${guild.Name}$[[Gray]]$!");
                        foreach (ITextChannel channel in guild.TextChannels)
                        {
                            var newChannel = new DiscordTextChannel(channel.Id.ToString(), channel.GuildId.ToString(), channel.Name, channel.Topic, channel.CreatedAt, ((channel as ISocketPrivateChannel) != null));
                            ConsoleEx.WriteColoredLine($"Joined new Channel $[[DarkCyan]]${channel.Name}$[[Gray]]$ within the Guild $[[DarkCyan]]${guild.Name}$[[Gray]]$!");
                        }
                    }
                    else
                    {
                        DBContext.DetachLocal(newGuild, existingGuild.Id);
                        ConsoleEx.WriteColoredLine($"Joined existing Guild $[[DarkCyan]]${guild.Name}$[[Gray]]$, updating data.");
                        await ProcessTextChannels(guild);
                        await ProcessUsers(guild);
                    }
                    await DBContext.SaveChangesAsync();
                }
            };

            Client.GuildUpdated += async(oldGuild, newGuild) =>
            {
                using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                {
                    DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                    DiscordGuild foundGuild = DBContext.Guilds.FromSql("SELECT * FROM Guilds WHERE Id = {0} LIMIT 1", oldGuild.Id.ToString()).AsNoTracking().First();
                    var          addedGuild = new DiscordGuild(newGuild.Id, newGuild.Name, newGuild.OwnerId, newGuild.CreatedAt, newGuild.IconUrl, newGuild.SplashUrl);
                    if (foundGuild == null)
                    {
                        await DBContext.AddAsync(addedGuild);
                        await ProcessTextChannels(newGuild);
                        await ProcessUsers(newGuild);
                    }
                    else
                    {
                        DBContext.DetachLocal(addedGuild, oldGuild.Id.ToString());
                        ConsoleEx.WriteColoredLine($"Updating existing Guild $[[DarkCyan]]${oldGuild.Name}$[[Gray]]$, adjusting record.");
                    }
                    await DBContext.SaveChangesAsync();
                }
            };

            Client.ChannelCreated += async(channel) =>
            {
                ITextChannel textChannel = channel as ITextChannel;
                if (textChannel == null)
                {
                    return;
                }

                using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                {
                    DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                    var existingTextChannel = DBContext.TextChannels.FromSql("SELECT * FROM TextChannels WHERE Id = {0} LIMIT 1", channel.Id.ToString()).AsNoTracking().FirstOrDefault();
                    if (existingTextChannel == null)
                    {
                        var newChannel = new DiscordTextChannel(textChannel.Id.ToString(), textChannel.GuildId.ToString(), textChannel.Name, textChannel.Topic, textChannel.CreatedAt, ((textChannel as ISocketPrivateChannel) != null));
                        await DBContext.AddAsync(newChannel);

                        ConsoleEx.WriteColoredLine($"Added new TextChannel $[[DarkCyan]]${textChannel.Name}$[[Gray]]$!");
                    }
                    else
                    {
                        DBContext.DetachLocal(existingTextChannel, existingTextChannel.Id);
                        ConsoleEx.WriteColoredLine($"Channel already exists somehow, $[[DarkCyan]]${textChannel.Name}$[[Gray]]$, updating records.");
                    }
                    await DBContext.SaveChangesAsync();
                }
            };

            Client.ChannelUpdated += async(oldChan, newChan) =>
            {
                ITextChannel oldTextChannel = oldChan as ITextChannel;
                ITextChannel newTextChannel = newChan as ITextChannel;
                if (oldTextChannel == null || newTextChannel == null)
                {
                    return;
                }

                using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                {
                    DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                    var existingChannel = DBContext.TextChannels.FromSql("SELECT * FROM TextChannels WHERE Id = {0} LIMIT 1", oldChan.Id.ToString()).FirstOrDefault();

                    var newChannel = new DiscordTextChannel(newTextChannel.Id.ToString(), newTextChannel.GuildId.ToString(), newTextChannel.Name, newTextChannel.Topic, newTextChannel.CreatedAt, ((newTextChannel as ISocketPrivateChannel) != null));
                    if (existingChannel == null)
                    {
                        await DBContext.AddAsync(newChannel);

                        ConsoleEx.WriteColoredLine($"Added new TextChannel $[[DarkCyan]]${newTextChannel.Name}$[[Gray]]$ that $[[Red]]$should$[[Gray]]$ have existed!");
                    }
                    else
                    {
                        DBContext.DetachLocal(newChannel, newTextChannel.Id.ToString());
                        ConsoleEx.WriteColoredLine($"Updating existing TextChannel $[[DarkCyan]]${oldTextChannel.Name}$[[Gray]]$, adjusting record.");
                    }
                    await DBContext.SaveChangesAsync();
                }
            };
        }
Ejemplo n.º 3
0
    void OnGUI()
    {
        if (!client.isOnline)
        {
            bot = GUILayout.Toggle(bot, "Bot");

            if (!bot)
            {
                email    = GUILayout.TextField(email);
                password = GUILayout.PasswordField(password, '*');
            }

            else
            {
                token = GUILayout.TextField(token);
            }

            if (GUILayout.Button("Start"))
            {
                if (!bot)
                {
                    client.Start(email, password, ClientOpened);
                }
                else
                {
                    client.StartBot(token, ClientOpened);
                }
            }
        }

        else
        {
            serverName  = GUILayout.TextField(serverName);
            channelName = GUILayout.TextField(channelName);
            memberName  = GUILayout.TextField(memberName);
            roleName    = GUILayout.TextField(roleName);

            editRole = GUILayout.Toggle(editRole, "Role");

            if (GUILayout.Button("Give Permission for specific channel"))
            {
                if (server == null)
                {
                    server = client.servers.Where(x => x.name == serverName).FirstOrDefault();
                }
                else if (server.name != serverName)
                {
                    server = client.servers.Where(x => x.name == serverName).FirstOrDefault();
                }
                if (channel == null)
                {
                    channel = server.channels.Where(x => x.name == channelName).FirstOrDefault();
                }
                else if (channel.name != channelName)
                {
                    channel = server.channels.Where(x => x.name == channelName).FirstOrDefault();
                }

                if (editRole)
                {
                    if (role == null)
                    {
                        role = server.roles.Where(x => x.name == roleName).FirstOrDefault();
                    }
                    else if (role.name != roleName)
                    {
                        role = server.roles.Where(x => x.name == roleName).FirstOrDefault();
                    }
                    Debug.Log((role == null));
                    Debug.Log((channel == null));
                    channel.OverwritePermissions(role, new DiscordPermission[1] {
                        DiscordPermission.SendMessages
                    }, new DiscordPermission[0], OnPermissionChanged);
                }

                else
                {
                    if (member == null)
                    {
                        member = server.members.Where(x => x.name == memberName).FirstOrDefault();
                    }
                    else if (member.name != memberName)
                    {
                        member = server.members.Where(x => x.name == memberName).FirstOrDefault();
                    }
                    channel.OverwritePermissions(member, new DiscordPermission[1] {
                        DiscordPermission.SendMessages
                    }, new DiscordPermission[0], OnPermissionChanged);
                }
            }

            if (GUILayout.Button("Remove Permission for specific channel"))
            {
                if (server == null)
                {
                    server = client.servers.Where(x => x.name == serverName).FirstOrDefault();
                }
                else if (server.name != serverName)
                {
                    server = client.servers.Where(x => x.name == serverName).FirstOrDefault();
                }
                if (channel == null)
                {
                    channel = server.channels.Where(x => x.name == channelName).FirstOrDefault();
                }
                else if (channel.name != channelName)
                {
                    channel = server.channels.Where(x => x.name == channelName).FirstOrDefault();
                }

                if (editRole)
                {
                    if (role == null)
                    {
                        role = server.roles.Where(x => x.name == roleName).FirstOrDefault();
                    }
                    else if (role.name != roleName)
                    {
                        role = server.roles.Where(x => x.name == roleName).FirstOrDefault();
                    }
                    channel.OverwritePermissions(role, new DiscordPermission[0], new DiscordPermission[1] {
                        DiscordPermission.SendMessages
                    }, OnPermissionChanged);
                }

                else
                {
                    if (member == null)
                    {
                        member = server.members.Where(x => x.name == memberName).FirstOrDefault();
                    }
                    else if (member.name != memberName)
                    {
                        member = server.members.Where(x => x.name == memberName).FirstOrDefault();
                    }
                    channel.OverwritePermissions(member, new DiscordPermission[0], new DiscordPermission[1] {
                        DiscordPermission.SendMessages
                    }, OnPermissionChanged);
                }
            }

            if (GUILayout.Button("Stop"))
            {
                client.Stop(ClientClosed);
            }
        }
    }