コード例 #1
0
		public async Task RegisterMsg(
			[Summary("ID of message you want to use a description for what theese roles do.")]ulong msgId,
			[Summary("ID or names for the roles you want selectable.")]params string[] roles)
		{
			IUserMessage msg = (IUserMessage)await Channel.GetMessageAsync(msgId);
			List<IRole> guildRoles = await GetRoles(roles);
			List<IEmote> emotes = msg.Reactions.Select(x => x.Key).ToList();

			if (roles.Length > emotes.Count)
			{
				await RespondAsync("Too many input roles, each reaction must have one role.", false, false);
				return;
			}

			if (roles.Length < emotes.Count)
			{
				await RespondAsync("Not enough input roles, each reaction must have one role.", false, false);
				return;
			}

			// Go through all reactions on target message and add the bots own reaction and sync DB
			for (int i = 0; i < emotes.Count; i++)
			{
				IEmote emote = emotes[i];
				IRole role = guildRoles[i];

				await DatabaseModule.RunNonQueryAsync(Config.RoleDatabase, $"INSERT INTO reactionMessages VALUES ({msg.Id}, '{emote.Name.GetHashCode()}', {role.Id});");
				await msg.AddReactionAsync(emote);
			}

			// Delete traces of this command being talked about.
			await Message.DeleteAsync();
		}
コード例 #2
0
        public static Module CreateModule(Host host, ModuleSettings settings)
        {
            Module module;

            switch (settings.Type)
            {
            case ModuleType.Frontend:
                module = new FrontendModule(host, settings);
                break;

            case ModuleType.Backend:
                module = new BackendModule(host, settings);
                break;

            case ModuleType.Database:
                module = new DatabaseModule(host, settings);
                break;

            case ModuleType.FileStorage:
                module = new FileStorageModule(host, settings);
                break;

            default:
                throw new NotSupportedException(string.Format("It does not support {0} = {1}.", typeof(ModuleType).Name, settings.Type));
            }

            return(module);
        }
コード例 #3
0
		/// <summary>
		/// Check if user has role.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="role"></param>
		/// <returns></returns>
		public async Task<bool> HasRoleAsync(IGuildUser user, string role)
		{
			IRole guildRole = (await GetRoles(new string[] { role }))[0];

			string hasRoleQuery = $"SELECT EXISTS(SELECT * FROM userRoles WHERE user_id = {user.Id} AND role_ID = {guildRole.Id});";
			List<DatabaseRow> hasRole = DatabaseModule.RunQuery(Config.RoleDatabase, hasRoleQuery);

			return hasRole[0].GetData<long>(0) == 1;
		}
コード例 #4
0
        /// <summary>
        /// Retrive all eve id's from the database.
        /// </summary>
        /// <returns></returns>
        async Task <Dictionary <ulong, int> > GetAuthedUsers()
        {
            Dictionary <ulong, int> dbUser = new Dictionary <ulong, int>();
            List <DatabaseRow>      rows   = await DatabaseModule.RunQueryAsync(Config.RecruitmentDatabase, "select * from users;");

            foreach (DatabaseRow row in rows)
            {
                dbUser.Add((ulong)row.GetData <long>(1), row.GetData <int>(0));
            }

            return(dbUser);
        }
コード例 #5
0
		/// <summary>
		/// Loop through all guild users and check if they have all assigned roles
		/// </summary>
		/// <returns></returns>
		public async Task VerifyRoles()
		{
			foreach (IGuildUser user in await Guild.GetUsersAsync())
			{
				// Get assigned roles for a user.
				List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM userRoles WHERE user_id = {user.Id};");
				List<ulong> roles = rows.Select(x => (ulong)x.Data[0]).ToList();

				// Check all roles user currently has.
				foreach (ulong role in user.RoleIds)
				{
					// Skip roles not configured to be managed.
					if (!Config.RolesToManage.Any(x => x.RoleID == role))
						continue;

					bool hasRole = user.RoleIds.Any(x => x == role);

					// Remove role from user if he does not have permission to have it.
					if (hasRole && !roles.Contains(role))
						await RemoveActualRole(user, role);

					// Add any roles to user if he does not have it.
					if (!hasRole && roles.Contains(role))
						await AddActualRole(user, role);
				}

				// Check all assigned roles.
				foreach (ulong role in roles)
				{
					// Skip roles not configured to be managed.
					if (!Config.RolesToManage.Any(x => x.RoleID == role))
						continue;

					bool hasRole = user.RoleIds.Any(x => x == role);

					// Remove role from user if he does not have permission to have it.
					if (hasRole && !roles.Contains(role))
						await RemoveActualRole(user, role);

					// Add any roles to user if he does not have it.
					if (!hasRole && roles.Contains(role))
						await AddActualRole(user, role);
				}
			}
		}
コード例 #6
0
		protected override async Task ReactionRemoved(IUserMessage message, ISocketMessageChannel channel, SocketReaction reaction)
		{
			List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT * FROM reactionMessages where message_id = {message.Id} LIMIT 1;");

			// Ignore if a bot is reacting or reaction is not on a reaction message
			if (reaction.User.Value.IsBot || rows.Count == 0)
				return;

			string emote = reaction.Emote.Name.GetHashCode().ToString();
			ulong roleID = await GetRoleID(message.Id, emote);

			// Get and set the context for this call because it comes from an event.
			IGuildUser user = await(message.Channel as IGuildChannel).GetUserAsync(reaction.User.Value.Id);
			SetContext(new MethodContext(user.Guild, channel, message));

			await ModifyRoleAsync(user, RoleAction.Remove, new string[] { roleID.ToString() });
			await VerifyRoles();
		}
コード例 #7
0
        /// <summary>
        /// Try to get the moon composition from a parsed moon.
        /// </summary>
        /// <param name="moon"></param>
        /// <param name="composition"></param>
        /// <returns></returns>
        bool TryGetMoon(SystemMoon moon, out MoonComposition composition)
        {
            composition = null;

            // Query for retriving a list of all ore types as well as the quantity for a moon.
            string moonDataQuery = $"SELECT c.type_name, b.quantity FROM mapdata a, moondata b, typedata c WHERE a.item_name = '{moon.Name}' AND b.moon_id = a.item_id AND c.type_id = b.type_id;";
            // Query for checking wether the moon station is a tatara or not.
            string isTataraQuery = $"SELECT EXISTS(SELECT * FROM moonrefinery a, mapdata b WHERE a.moon_id = b.item_id and b.item_name = '{moon.Name}');";

            List <DatabaseRow> moonData = DatabaseModule.RunQuery(Config.Database, moonDataQuery);
            List <DatabaseRow> isTatara = DatabaseModule.RunQuery(Config.Database, isTataraQuery);

            if (moonData.Count == 0)
            {
                return(false);
            }

            composition = new MoonComposition(moon, isTatara[0].GetData <long>(0) == 1, moonData);
            return(true);
        }
コード例 #8
0
		/// <summary>
		/// Syncronize the discord roles and the databse roles.
		/// </summary>
		/// <returns></returns>
		public async Task SyncRoles()
		{
			foreach (IGuildUser user in await Guild.GetUsersAsync())
			{
				// Get assigned roles for a user.
				List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM userRoles WHERE user_id = {user.Id};");
				List<ulong> roles = rows.Select(x => (ulong)x.Data[0]).ToList();

				foreach (ulong role in user.RoleIds)
				{
					// Skip roles not configured to be managed.
					if (!Config.RolesToManage.Any(x => x.RoleID == role))
						continue;

					IRole guildRole = Guild.GetRole(role);
					bool hasRole = user.RoleIds.Any(x => x == role);
					await ModifyRoleAsync(user, hasRole ? RoleAction.Add : RoleAction.Remove, guildRole.Name);
				}
			}
		}
コード例 #9
0
		/// <summary>
		/// Change a role status for a user. (Will ignore any roles that are not manages)
		/// </summary>
		/// <param name="user"></param>
		/// <param name="action"></param>
		/// <param name="roles"></param>
		/// <returns></returns>
		public async Task ModifyRoleAsync(IGuildUser user, RoleAction action, params string[] roles)
		{
			// TODO: Fix manual add settings.
			foreach (IRole role in await GetRoles(roles))
			{
				// Skip roles that are not managed.
				if (!Config.RolesToManage.Any(x => x.RoleID == role.Id))
				{
					await LogAsync(LogLevel.Warning, "Trying to modify role that has not been configured to be manages.");
					continue;
				}

				// Take appropriate action based on input.
				switch (action)
				{
					case RoleAction.Add:
						await DatabaseModule.RunNonQueryAsync(Config.RoleDatabase, $"INSERT INTO userRoles VALUES ({user.Id}, {role.Id});");
						break;
					case RoleAction.Remove:
						await DatabaseModule.RunNonQueryAsync(Config.RoleDatabase, $"DELETE FROM userRoles where user_id = {user.Id} AND role_id = {role.Id}");
						break;
					case RoleAction.AddManual:
						ManagedRole managedRole = Config.RolesToManage.First(x => x.RoleID == role.Id);

						if (managedRole.ManualAdd)
							await DatabaseModule.RunNonQueryAsync(Config.RoleDatabase, $"INSERT INTO userRoles VALUES ({user.Id}, {role.Id});");
						break;
					case RoleAction.RemoveManual:
						managedRole = Config.RolesToManage.First(x => x.RoleID == role.Id);

						if (managedRole.ManualAdd)
							await DatabaseModule.RunNonQueryAsync(Config.RoleDatabase, $"DELETE FROM userRoles where user_id = {user.Id} AND role_id = {role.Id}");
						break;
					default:
						break;
				}
			}
		}
コード例 #10
0
        public async Task Auth(
            [Summary("Discord user ID, Enable developer mode and rick click user.")] ulong discordId,
            [Summary("Eve user ID, Go to thei ZKill page and copy the ID from the URL.")] int eveId)
        {
            IGuildUser user = await Guild.GetUserAsync(discordId);

            Dictionary <ulong, int> authedUsers = await GetAuthedUsers();

            if (authedUsers.TryGetValue(discordId, out int authedChar))
            {
                await RespondAsync($"User already authed to {authedChar}, if bot does not update roles run !namecheck or !auth loop restart. If that does not work pray Prople is online.", false, false);

                return;
            }

            string name = string.IsNullOrEmpty(user.Nickname) ? user.Username : user.Nickname;

            await RespondAsync($"Authenticating {name}...", false, false);

            await DatabaseModule.RunQueryAsync(Config.RecruitmentDatabase, $"INSERT INTO users VALUES ({eveId},{discordId})");

            await RespondAsync("Done, run !namecheck to update roles.", false, false);
        }
コード例 #11
0
		/// <summary>
		/// Resolve reaction message emote to get role id.
		/// </summary>
		/// <param name="msgId"></param>
		/// <param name="emote"></param>
		/// <returns></returns>
		async Task<ulong> GetRoleID(ulong msgId, string emote)
		{
			List<DatabaseRow> rows = await DatabaseModule.RunQueryAsync(Config.RoleDatabase, $"SELECT role_id FROM reactionMessages where message_id = {msgId} AND emote = '{emote}' LIMIT 1;");
			return (ulong)rows[0].Data[0];
		}