Пример #1
0
        /// <summary>
        /// Adds a member to a guild
        /// </summary>
        /// <param name="guild">Guild the new member joins</param>
        /// <param name="newMember">New member that joins the guild</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> MemberJoinGuildAsync(MinecraftGuild guild, SocketGuildUser newMember)
        {
            string errorhint = "Adding Guild Role";

            try
            {
                if (BotCore.Client.TryGetRole(guild.RoleId, out SocketRole guildRole) && !guild.MemberIds.Contains(newMember.Id))
                {
                    await newMember.AddRoleAsync(guildRole);

                    errorhint = "Adding Member to Guild and Saving";
                    guild.MemberIds.Add(newMember.Id);
                    await SaveAll();

                    errorhint = "Notify Admins";
                    await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Add user \"{newMember}\" to guild \"{guild.Name}\" ingame", "Joining User: "******"Error joining player {newMember?.Mention} to guild {guild?.Name}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Changes a guilds color
        /// </summary>
        /// <param name="guild">Guild to update</param>
        /// <param name="newColor">New color to apply</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> UpdateGuildColorAsync(MinecraftGuild guild, GuildColor newColor)
        {
            string errorhint = "Modifying Guild Role";

            try
            {
                if (BotCore.Client.TryGetRole(guild.RoleId, out SocketRole guildRole))
                {
                    await guildRole.ModifyAsync(RoleProperties =>
                    {
                        RoleProperties.Color = MinecraftGuild.ToDiscordColor(newColor);
                    });

                    errorhint   = "Setting Guild Color";
                    guild.Color = newColor;
                    errorhint   = "Notify Admins";
                    await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Change color of guild \"{guild.Name}\" to \"{newColor}\" ingame", $"Color: `{ newColor}` (`0x{ ((uint)newColor).ToString("X")}`)");

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error recoloring guild {guild.Name} to {newColor}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #3
0
 /// <summary>
 /// Attempts to find a guild
 /// </summary>
 /// <param name="id">Id of the user to check guild membership for</param>
 /// <param name="guild">Guild result</param>
 /// <param name="invalidDatasets">Wether to include guilds where name and color could not be sourced (Role not found)</param>
 /// <returns>True, if a result was found</returns>
 public static bool TryGetGuildOfUser(ulong id, out MinecraftGuild guild, bool invalidDatasets = false)
 {
     foreach (MinecraftGuild item in guilds)
     {
         if (item.CaptainId == id || item.MemberIds.Contains(id) || item.MateIds.Contains(id))
         {
             guild = item;
             return(guild.NameAndColorFound || invalidDatasets);
         }
     }
     guild = null;
     return(false);
 }
Пример #4
0
 /// <summary>
 /// Attempts to find a guild
 /// </summary>
 /// <param name="name">Name filter to select a guild</param>
 /// <param name="guild">Guild result</param>
 /// <param name="invalidDatasets">Wether to include guilds where name and color could not be sourced (Role not found)</param>
 /// <returns>True, if a result was found</returns>
 public static bool TryGetGuild(string name, out MinecraftGuild guild, bool invalidDatasets = false)
 {
     foreach (MinecraftGuild item in guilds)
     {
         item.TryFindNameAndColor();
         if (string.Equals(item.Name, name, StringComparison.InvariantCultureIgnoreCase))
         {
             guild = item;
             return(guild.NameAndColorFound || invalidDatasets);
         }
     }
     guild = null;
     return(false);
 }
Пример #5
0
        /// <summary>
        /// Appoints a new guild captain to a guild
        /// </summary>
        /// <param name="guild">Guild to set the captain for</param>
        /// <param name="newCaptain">New captain to appoint to the guild</param>
        /// <param name="oldCaptain">Old captain (can be null)</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> SetGuildCaptain(MinecraftGuild guild, SocketGuildUser newCaptain, SocketGuildUser oldCaptain)
        {
            string errorhint = "Modify channel perms";

            try
            {
                SocketRole captainRole = newCaptain.Guild.GetRole(SettingsModel.GuildCaptainRole);
                if (GuildChannelHelper.TryGetChannel(guild.ChannelId, out SocketTextChannel guildChannel))
                {
                    if (oldCaptain != null)
                    {
                        await guildChannel.RemovePermissionOverwriteAsync(oldCaptain);
                    }
                    await guildChannel.AddPermissionOverwriteAsync(newCaptain, CaptainChannelPerms);
                }
                errorhint = "Modify and save";
                if (captainRole != null)
                {
                    await newCaptain.AddRoleAsync(captainRole);
                }
                if (oldCaptain != null)
                {
                    if (captainRole != null)
                    {
                        await oldCaptain.RemoveRoleAsync(captainRole);
                    }
                    guild.MemberIds.Add(guild.CaptainId);
                }
                guild.MemberIds.Remove(newCaptain.Id);
                guild.MateIds.Remove(newCaptain.Id);
                guild.CaptainId = newCaptain.Id;
                await SaveAll();

                return(true);
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error setting captain for {guild.Name} to {newCaptain.Mention}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #6
0
        public static async Task <bool> DemoteGuildMember(MinecraftGuild guild, SocketGuildUser newMate)
        {
            try
            {
                guild.MateIds.RemoveAll((ulong Id) =>
                {
                    return(Id == newMate.Id);
                });
                guild.MemberIds.Add(newMate.Id);
                await SaveAll();

                return(true);
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error promoting {newMate.Mention} to mate rank in guild \"{guild.Name}\".");

                return(false);
            }
        }
Пример #7
0
        /// <summary>
        /// Deletes a guild both on server and in data
        /// </summary>
        /// <param name="minecraftGuild">Guild to remove</param>
        /// <returns>True, if operation completed</returns>
        public static async Task <bool> DeleteGuildAsync(SocketGuild discordGuild, MinecraftGuild minecraftGuild)
        {
            string errorhint = "Removing Guild Role";

            try
            {
                SocketRole guildRole   = discordGuild.GetRole(minecraftGuild.RoleId);
                SocketRole captainRole = discordGuild.GetRole(SettingsModel.GuildCaptainRole);
                if (guildRole != null)
                {
                    await guildRole.DeleteAsync();
                }
                errorhint = "Removing captain role from captain";
                SocketGuildUser captain = discordGuild.GetUser(minecraftGuild.CaptainId);
                if (captain != null && captainRole != null)
                {
                    if (captain.Roles.Any(item => { return(item.Id == captainRole.Id); }))
                    {
                        await captain.RemoveRoleAsync(captainRole);
                    }
                }
                errorhint = "Removing Guild Channel";
                if (GuildChannelHelper.TryGetChannel(minecraftGuild.ChannelId, out SocketTextChannel guildChannel))
                {
                    await guildChannel.DeleteAsync();
                }
                errorhint = "Removing Guild and Saving";
                await DeleteGuildDatasetAsync(minecraftGuild);

                errorhint = "Notify Admins";
                await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Remove ingame represantation of guild\"{minecraftGuild.Name}\"", string.Empty);

                return(true);
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error removing guild {minecraftGuild.Name}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #8
0
        /// <summary>
        /// Changes a guilds name
        /// </summary>
        /// <param name="guild">Guild to modify</param>
        /// <param name="newName">New name for the guild</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> UpdateGuildNameAsync(MinecraftGuild guild, string newName)
        {
            string errorhint = "Modifying Guild Channel";

            try
            {
                if (GuildChannelHelper.TryGetChannel(guild.ChannelId, out SocketTextChannel guildChannel) && BotCore.Client.TryGetRole(guild.RoleId, out SocketRole guildRole))
                {
                    await guildChannel.ModifyAsync(GuildChannelProperties =>
                    {
                        GuildChannelProperties.Name  = newName;
                        GuildChannelProperties.Topic = "Private Guild Channel for " + newName;
                    });

                    errorhint = "Modifying Guild Role";
                    await guildRole.ModifyAsync(RoleProperties =>
                    {
                        RoleProperties.Name = newName;
                    });

                    errorhint = "Setting Guild Name";
                    string oldname = guild.Name;
                    guild.Name = newName;
                    errorhint  = "Notify Admins";
                    await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Rename guild \"{oldname}\" to \"{newName}\" ingame", string.Empty);

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error renaming guild {guild.Name} to {newName}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #9
0
        public static async Task Load()
        {
            var fileOperation = await ResourcesModel.LoadToJSONObject(ResourcesModel.GuildsFilePath);

            if (fileOperation.Success)
            {
                if (fileOperation.Result.IsArray)
                {
                    foreach (JSONField guild_json in fileOperation.Result.Array)
                    {
                        if (guild_json.IsObject)
                        {
                            MinecraftGuild guild = new MinecraftGuild();
                            if (guild.FromJSON(guild_json.Container))
                            {
                                guilds.Add(guild);
                            }
                        }
                    }
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Removes a member from a guild
        /// </summary>
        /// <param name="guild">Guild the member leaves</param>
        /// <param name="leavingMember">Member that leaves</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> MemberLeaveGuildAsync(MinecraftGuild guild, SocketGuildUser leavingMember)
        {
            string errorhint = "Removing Guild Role";

            try
            {
                if (guild.MemberIds.Contains(leavingMember.Id) || guild.MateIds.Contains(leavingMember.Id))
                {
                    foreach (SocketRole role in leavingMember.Roles)
                    {
                        if (role.Id == guild.RoleId)
                        {
                            await leavingMember.RemoveRoleAsync(role);

                            break;
                        }
                    }
                    errorhint = "Removing Member from Guild and Saving";
                    guild.MemberIds.Remove(leavingMember.Id);
                    guild.MateIds.Remove(leavingMember.Id);
                    await SaveAll();

                    errorhint = "Notify Admins";
                    await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Remove user \"{leavingMember}\" from guild \"{guild.Name}\" ingame", "Leaving User: "******"Error leaving player {leavingMember?.Mention} from guild {guild.Name}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #11
0
        public static async Task <bool> SetGuildActive(SocketGuild discordGuild, MinecraftGuild minecraftGuild, bool active)
        {
            string errorhint = string.Empty;

            try
            {
                List <ulong> MemberIds = new List <ulong>();
                MemberIds.Add(minecraftGuild.CaptainId);
                MemberIds.AddRange(minecraftGuild.MateIds);
                MemberIds.AddRange(minecraftGuild.MemberIds);

                SocketRole      guildRole   = discordGuild.GetRole(minecraftGuild.RoleId);
                SocketGuildUser captain     = discordGuild.GetUser(minecraftGuild.CaptainId);
                SocketRole      captainRole = discordGuild.GetRole(SettingsModel.GuildCaptainRole);

                if (active)
                {
                    errorhint = "Adding guild role to members";
                    if (guildRole != null)
                    {
                        foreach (ulong memberId in MemberIds)
                        {
                            SocketGuildUser member = discordGuild.GetUser(memberId);
                            if (member != null && !member.Roles.Any((role) => { return(role.Id == guildRole.Id); }))
                            {
                                await member.AddRoleAsync(guildRole);
                            }
                        }
                    }
                    errorhint = "Adding Captain Role to Captain";
                    if (captain != null && captainRole != null)
                    {
                        if (!captain.Roles.Any((role) => { return(role.Id == captainRole.Id); }))
                        {
                            await captain.AddRoleAsync(captainRole);
                        }
                    }
                }
                else
                {
                    errorhint = "Removing guild role from members";
                    if (guildRole != null)
                    {
                        foreach (ulong memberId in MemberIds)
                        {
                            SocketGuildUser member = discordGuild.GetUser(memberId);
                            if (member != null && member.Roles.Any((role) => { return(role.Id == guildRole.Id); }))
                            {
                                await member.RemoveRoleAsync(guildRole);
                            }
                        }
                    }
                    errorhint = "Removing Captain Role from Captain";
                    if (captain != null && captainRole != null)
                    {
                        if (captain.Roles.Any((role) => { return(role.Id == captainRole.Id); }))
                        {
                            await captain.RemoveRoleAsync(captainRole);
                        }
                    }
                }
                errorhint             = "Updating Dataset";
                minecraftGuild.Active = active;
                await SaveAll();

                return(true);
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error setting guild \"{minecraftGuild.Name}\" {(active ? "active" : "inactive")}. Errorhint: {errorhint}");

                return(false);
            }
        }
Пример #12
0
 public DeleteGuildDatasetOption(MinecraftGuild guild)
 {
     Guild       = guild;
     Description = $"Delete dataset of guild \"{guild.Name}\"";
 }
Пример #13
0
        /// <summary>
        /// Creates a new guild (includes role, channel, etc)
        /// </summary>
        /// <param name="guild">Discord Server Guild to create channel and role on</param>
        /// <param name="name">Guild Name</param>
        /// <param name="color">Guild Display Color</param>
        /// <param name="captain">Guild Captain</param>
        /// <param name="members">Guild Users</param>
        /// <returns>true, if operation succeeds</returns>
        public static async Task <bool> CreateGuildAsync(SocketGuild guild, string name, GuildColor color, SocketGuildUser captain, List <SocketGuildUser> members)
        {
            string errorhint = "Failed a precheck";

            try
            {
                if (TryGetGuildOfUser(captain.Id, out MinecraftGuild existingCaptainGuild))
                {
                    if (existingCaptainGuild.Active || captain.Id == existingCaptainGuild.CaptainId)
                    {
                        errorhint = "Precheck failed on " + captain.Mention;
                        return(false);
                    }
                    else
                    {
                        existingCaptainGuild.MateIds.Remove(captain.Id);
                        existingCaptainGuild.MemberIds.Remove(captain.Id);
                    }
                }
                foreach (SocketGuildUser member in members)
                {
                    if (TryGetGuildOfUser(member.Id, out MinecraftGuild existingMemberGuild))
                    {
                        if (existingCaptainGuild.Active || member.Id == existingCaptainGuild.CaptainId)
                        {
                            return(false);
                        }
                        else
                        {
                            errorhint = "Precheck failed on " + member.Mention;
                            existingCaptainGuild.MateIds.Remove(member.Id);
                            existingCaptainGuild.MemberIds.Remove(member.Id);
                        }
                    }
                }
                errorhint = "Failed to create Guild Role!";
                RestRole guildRole = await guild.CreateRoleAsync(name, color : MinecraftGuild.ToDiscordColor(color), isHoisted : true);

                errorhint = "Move role into position";
                await guildRole.ModifyAsync(RoleProperties =>
                {
                    RoleProperties.Position = GUILD_ROLE_POSITION;
                });

                errorhint = "Failed to create Guild Channel!";
                SocketCategoryChannel guildCategory = guild.GetChannel(GuildChannelHelper.GuildCategoryId) as SocketCategoryChannel;
                if (guildCategory == null)
                {
                    throw new Exception("Could not find Guild Category Channel!");
                }
                RestTextChannel guildChannel = await guild.CreateTextChannelAsync(name, TextChannelProperties =>
                {
                    TextChannelProperties.CategoryId = GuildChannelHelper.GuildCategoryId;
                    TextChannelProperties.Topic      = "Private Guild Channel for " + name;
                });

                errorhint = "Failed to copy guildcategories permissions";
                foreach (Overwrite overwrite in guildCategory.PermissionOverwrites)
                {
                    IRole role = guild.GetRole(overwrite.TargetId);
                    if (role != null)
                    {
                        await guildChannel.AddPermissionOverwriteAsync(role, overwrite.Permissions);
                    }
                }
                errorhint = "Failed to set Guild Channel Permissions!";
                await guildChannel.AddPermissionOverwriteAsync(guildRole, GuildRoleChannelPerms);

                await guildChannel.AddPermissionOverwriteAsync(captain, CaptainChannelPerms);

                errorhint = "Failed to add Guild Role to Captain!";
                await captain.AddRoleAsync(guildRole);

                errorhint = "Failed to add GuildCaptain Role to Captain!";
                SocketRole captainRole = guild.GetRole(SettingsModel.GuildCaptainRole);
                if (captainRole != null)
                {
                    await captain.AddRoleAsync(captainRole);
                }
                errorhint = "Failed to add Guild Role to a Member!";
                foreach (SocketGuildUser member in members)
                {
                    await member.AddRoleAsync(guildRole);
                }
                errorhint = "Failed to create MinecraftGuild!";

                StringBuilder memberPingString = new StringBuilder();

                MinecraftGuild minecraftGuild = new MinecraftGuild(guildChannel.Id, guildRole.Id, color, name, captain.Id);
                for (int i = 0; i < members.Count; i++)
                {
                    SocketGuildUser member = members[i];
                    minecraftGuild.MemberIds.Add(member.Id);
                    memberPingString.Append(member.Mention);
                    if (i < members.Count - 1)
                    {
                        memberPingString.Append(", ");
                    }
                }
                guilds.Add(minecraftGuild);
                errorhint = "Failed to save MinecraftGuild!";
                await SaveAll();

                errorhint = "Failed to send or pin guild info embed";
                var infomessage = await guildChannel.SendMessageAsync(embed : GuildHelpEmbed.Build());

                await infomessage.PinAsync();

                errorhint = "Notify Admins";
                await AdminTaskInteractiveMessage.CreateAdminTaskMessage($"Create ingame represantation for guild \"{name}\"", $"Name: `{name}`, Color: `{color}` (`0x{((uint)color).ToString("X")}`)\nCaptain: {captain.Mention}\nMembers: {memberPingString}");

                return(true);
            }
            catch (Exception e)
            {
                await GuildChannelHelper.SendExceptionNotification(e, $"Error creating guild {name}. Hint: {errorhint}");

                return(false);
            }
        }
Пример #14
0
 public AddUserToDatasetOption(MinecraftGuild guild, ulong userId)
 {
     Guild       = guild;
     UserId      = userId;
     Description = $"Add {Markdown.Mention_User(userId)} to dataset of guild \"{guild.Name}\"";
 }
Пример #15
0
 /// <summary>
 /// Removes the guild represantation in the guilds list and saves
 /// </summary>
 /// <param name="guild">Guild to remove</param>
 public static Task DeleteGuildDatasetAsync(MinecraftGuild guild)
 {
     guilds.Remove(guild);
     return(SaveAll());
 }
Пример #16
0
 public static bool TryParseMinecraftGuild(string argument, out MinecraftGuild minecraftGuild)
 {
     return(TryGetGuild(argument, out minecraftGuild, true));
 }
Пример #17
0
 public RemoveMemberDatasetDesyncOption(MinecraftGuild guild, ulong userId)
 {
     Guild       = guild;
     UserId      = userId;
     Description = $"Remove {userId} from memberlist of Guild \"{guild.Name}\"";
 }