Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Handles the action
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public async Task HandleAction(MessageInteractionContext context)
 {
     try
     {
         if (InteractiveMessageService.HasInteractiveMessage(MessageId))
         {
             if (InvalidateMessage)
             {
                 InteractiveMessageService.RemoveInteractiveMessage(MessageId);
             }
             if (await Action(context))
             {
                 InteractiveMessageService.RemoveInteractiveMessage(MessageId);
             }
         }
     }
     catch (Exception e)
     {
         await GuildChannelHelper.SendExceptionNotification(e, $"Error handling EmoteInteraction, MessageId `{MessageId}`, Emote {Emote.Name}");
     }
 }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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);
            }
        }
Esempio n. 9
0
        private async void Run()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            while (!closeThreadAfterWork)
            {
                if (Task != null)
                {
                    if (Task.TaskState == WorkerTaskState.Ready)
                    {
                        State = ThreadState.Working;
                        try
                        {
                            await Task.Run();
                        }
                        catch (Exception e)
                        {
                            await GuildChannelHelper.SendExceptionNotification(e, "A WorkerTask has crashed");

                            Task = null;
                        }
                        State = ThreadState.Idle;
                    }
                    else
                    {
                        Task = null;
                    }
                }
                else if (WorkerThreadService.HasQueuedTask)
                {
                    Task = WorkerThreadService.DeQueue();
                }
                else
                {
                    Thread.Sleep(1);
                }
            }
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
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);
            }
        }
Esempio n. 12
0
        private static async void CheckTimeLimitedInfractions()
        {
            TimeSpan waitDelay = TimeSpan.FromMinutes(1);

            Thread.Sleep(waitDelay);
            while (true)
            {
                foreach (UserModerationLog userModlog in timeLimitedInfractions)
                {
                    try
                    {
                        if (userModlog.IsBanned)
                        {
                            if (userModlog.BannedUntil.Value < DateTimeOffset.UtcNow)
                            {
                                // Unban User
                                await UnbanUser(userModlog);

                                lock (removelistlock)
                                {
                                    toBeRemoved.Add(userModlog);
                                }
                            }
                        }
                        else if (userModlog.IsMuted)
                        {
                            if (userModlog.MutedUntil.Value < DateTimeOffset.UtcNow)
                            {
                                // Unmute User
                                await UnMuteUser(userModlog);

                                lock (removelistlock)
                                {
                                    toBeRemoved.Add(userModlog);
                                }
                            }
                        }
                        else
                        {
                            lock (removelistlock)
                            {
                                toBeRemoved.Add(userModlog);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        await GuildChannelHelper.SendExceptionNotification(e, string.Empty);
                    }
                }
                lock (removelistlock)
                {
                    timeLimitedInfractions.RemoveRange(toBeRemoved);
                    toBeRemoved.Clear();
                }
                lock (addlistlock)
                {
                    timeLimitedInfractions.AddRange(toBeAdded);
                    toBeAdded.Clear();
                }
                Thread.Sleep(waitDelay);
            }
        }