示例#1
0
        /// <summary>
        /// Builds and adds role to server. IF the operation fails, an exception will be thrown
        /// with the error message.
        /// </summary>
        public Discord.Rest.RestRole Build()
        {
            bool hexColorUsed      = false;
            bool positionedByIndex = false;

            if (altColor != null)
            {
                hexColorUsed = true;
            }
            if (pos != -1)
            {
                positionedByIndex = true;
            }
            else if (upperRole != "" || lowerRole != "")
            {
                this.pos          = GetIndexForRelativePos();
                positionedByIndex = true;
            }
            try {
                GeneratePermRawValue();
            } catch (Exception e) { throw e; }

            GuildPermissions perms = new GuildPermissions(permVal);

            Discord.Rest.RestRole role = null;
            if (hexColorUsed)
            {
                try {
                    Color localColor = new Color(altColor[0], altColor[1], altColor[2]);
                    role = guild.CreateRoleAsync(
                        name, perms, localColor, isHoisted
                        ).Result;
                }catch (Exception e) { throw e; }
            }
            else
            {
                try {
                    role = guild.CreateRoleAsync(
                        name, perms, color, isHoisted
                        ).Result;
                }catch (Exception e) { throw e; }
            }

            if (positionedByIndex)
            {
                //(roles count - pos) since this class rests on supposition
                //that top most role is 0, and not bottom as discord API does
                ReorderRoleProperties x = new
                                          ReorderRoleProperties(role.Id, guild.Roles.Count() - pos);
                IEnumerable <ReorderRoleProperties> roleNewPos =
                    (IEnumerable <ReorderRoleProperties>)
                        (new List <ReorderRoleProperties> {
                    x
                });
                guild.ReorderRolesAsync(roleNewPos);
            }
            return(role);
        }
示例#2
0
        public async Task SetupLevel()
        {
            await Context.Message.DeleteAsync();

            using (var db = Database.Open())
            {
                var roles = db.Roles.AsQueryable().Where(p => p.GuildId == Context.Guild.Id);
                if (roles.FirstOrDefault(p => p.Description == "Grandmaster") == null)
                {
                    Discord.Rest.RestRole roleS4 = await Context.Guild.CreateRoleAsync("Grandmaster", null, new Color(48, 45, 37), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = roleS4.Id, Description = "Grandmaster" });
                }
                if (roles.FirstOrDefault(p => p.Description == "Master") == null)
                {
                    Discord.Rest.RestRole roleS4 = await Context.Guild.CreateRoleAsync("Master", null, new Color(239, 69, 50), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = roleS4.Id, Description = "Master" });
                }
                if (roles.FirstOrDefault(p => p.Description == "Expert") == null)
                {
                    Discord.Rest.RestRole rolePro = await Context.Guild.CreateRoleAsync("Expert", null, new Color(239, 69, 50), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = rolePro.Id, Description = "Expert" });
                }
                if (roles.FirstOrDefault(p => p.Description == "Pro") == null)
                {
                    Discord.Rest.RestRole roleSemi = await Context.Guild.CreateRoleAsync("Pro", null, new Color(94, 137, 255), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = roleSemi.Id, Description = "Pro" });
                }
                if (roles.FirstOrDefault(p => p.Description == "Semi") == null)
                {
                    Discord.Rest.RestRole roleAmateur = await Context.Guild.CreateRoleAsync("Semi", null, new Color(21, 216, 102), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = roleAmateur.Id, Description = "Semi" });
                }
                if (roles.FirstOrDefault(p => p.Description == "Rookie") == null)
                {
                    Discord.Rest.RestRole roleRookie = await Context.Guild.CreateRoleAsync("Rookie", null, new Color(219, 199, 164), true, false);

                    await db.Roles.AddAsync(new RoleEntity { GuildId = Context.Guild.Id, RoleId = roleRookie.Id, Description = "Rookie" });
                }
                await db.SaveChangesAsync();
            }
        }
示例#3
0
        public async Task SetInClass(HwkUser user, [Remainder] string class_)
        {
            class_ = class_.Replace("Maths", "Mathematics");
            class_ = class_.Replace("RE", "R.E.");
            Class cls = null;

            if (Classes.TryGetValue(class_, out cls))
            {
            }
            else
            {
                Discord.Rest.RestRole role = await Program.TheGrandCodingGuild.CreateRoleAsync(class_);

                SocketRole sRole = role.GetSocketRole(500, Program.TheGrandCodingGuild);
                cls = new Class(sRole);
                Classes.Add(cls.Name, cls);
            }
            if (user.Classes.Contains(cls.Name))
            {
                cls.Users.Remove(user);
                user.Classes.Remove(cls.Name);
                await user.User.RemoveRoleAsync(cls.Role);

                if (user.Classes.Count == 0)
                {
                    await user.User.RemoveRoleAsync(Separator);
                }
                await ReplyAsync($"Removed {user.Name} from {cls.Name}");
            }
            else
            {
                cls.Users.Add(user);
                user.Classes.Add(cls.Name);
                await user.User.AddRoleAsync(cls.Role);

                await user.User.AddRoleAsync(Separator);
                await ReplyAsync($"Added {user.Name} to {cls.Name}");
            }
        }
示例#4
0
            public async Task <RuntimeResult> Setup(IGuildUser user, [Remainder] String arg = "")
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(MySQLConnString.Get()))
                    {
                        var mention = user.Mention;

                        string resultString = $"Setting up {mention}:\n";

                        //User ID section
                        int userID = DatabaseHandler.GetUserID(user.Id);

                        //If the user doesn't exist, create and insert him.
                        if (userID == 0)
                        {
                            userID        = DatabaseHandler.AddUser(user.Id, Context.Guild.Id, user.Username);
                            resultString += "User ID created.\n";
                        }
                        else
                        {
                            resultString += "User ID already exists.\n";
                        }

                        //Personal role section
                        ulong result = DatabaseHandler.GetPersonalRole(userID);

                        Discord.Rest.RestRole role = null;

                        //Check if we got a result.
                        if (result == 0)
                        {   //No result was found
                            var name = user.Username.ToString();

                            //Create the role in discord.
                            role = await Context.Guild.CreateRoleAsync(name);

                            //Add the role to the user who called this command.
                            await((IGuildUser)user).AddRoleAsync(role);

                            //Add the role to the database
                            DatabaseHandler.AddPersonalRole(userID, role.Id);

                            //Command complete.
                            resultString += "Personal role created.\n";

                            if (arg != "")
                            {
                                arg = arg.TrimStart('#');

                                await role.ModifyAsync(x =>
                                {
                                    x.Color = new Color(Convert.ToUInt32(arg, 16));
                                });

                                resultString += "I set your color for you.\n";
                            }
                        }
                        else
                        {
                            resultString += "Personal role already exists.\n";
                        }

                        //Personal Channel section
                        ulong personalChannel = DatabaseHandler.GetPersonalChannel(userID);

                        if (personalChannel == 0)
                        {
                            ulong adminBotRoleID = DatabaseHandler.GetAdminbotRoleID();

                            var adminBot = Context.Guild.GetRole(adminBotRoleID);

                            var newChannel = await Context.Guild.CreateTextChannelAsync(user.Username.ToString(), x =>
                            {
                                x.Topic    = $"Your personal channel to talk to the bot.";
                                x.Position = 2;
                            });

                            var newChannelID = newChannel.Id;

                            var everyone = Context.Guild.EveryoneRole;

                            await newChannel.AddPermissionOverwriteAsync(adminBot, new Discord.OverwritePermissions(viewChannel: Discord.PermValue.Allow));

                            await newChannel.AddPermissionOverwriteAsync(role, new Discord.OverwritePermissions(viewChannel: Discord.PermValue.Allow));

                            await newChannel.AddPermissionOverwriteAsync(everyone, new Discord.OverwritePermissions(viewChannel: Discord.PermValue.Deny));

                            await newChannel.AddPermissionOverwriteAsync(role, new Discord.OverwritePermissions(manageMessages: Discord.PermValue.Allow));

                            DatabaseHandler.AddPersonalChannel(userID, newChannelID);
                            resultString += "Channel created.\n";
                        }
                        else
                        {
                            resultString += "Channel already exists.\n";
                        }

                        await Context.Channel.SendMessageAsync(resultString);

                        return(WestbotCommandResult.AcceptReact(null, true));
                    }
                }
                catch (Exception ex)
                {   //Display exception.
                    Console.WriteLine("Exception: " + ex.Message);
                    await Context.Channel.SendMessageAsync("Exception: " + ex.Message);

                    return(WestbotCommandResult.ErrorReact(null, true));
                }
            }