assign_role
Assign a specific role to a Discord server member using guild, member, and role IDs to manage permissions and organization.
Instructions
Assign a role to a member
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) | |
| memberId | Yes | The ID of the member | |
| roleId | Yes | The ID of the role to assign | |
| reason | No | Reason for assigning the role |
Implementation Reference
- src/tools/role-tools.ts:219-242 (handler)The handler function for the 'assign_role' tool. It fetches the Discord guild, member, and role, then adds the role to the member using `member.roles.add()`. Handles errors with `withErrorHandling` and formats the response as JSON or error text.async ({ guildId, memberId, roleId, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const member = await guild.members.fetch(memberId); const role = await guild.roles.fetch(roleId); if (!role) throw new Error('Role not found'); await member.roles.add(role, reason); return { memberId, roleId, roleName: role.name, memberName: member.displayName, message: 'Role assigned successfully', }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; }
- src/tools/role-tools.ts:213-218 (schema)Zod input schema defining parameters for the 'assign_role' tool: required guildId, memberId, roleId; optional reason.{ guildId: z.string().describe('The ID of the server (guild)'), memberId: z.string().describe('The ID of the member'), roleId: z.string().describe('The ID of the role to assign'), reason: z.string().optional().describe('Reason for assigning the role'), },
- src/tools/role-tools.ts:210-243 (registration)The server.tool() call that registers the 'assign_role' tool on the MCP server, including description, input schema, and handler function.server.tool( 'assign_role', 'Assign a role to a member', { guildId: z.string().describe('The ID of the server (guild)'), memberId: z.string().describe('The ID of the member'), roleId: z.string().describe('The ID of the role to assign'), reason: z.string().optional().describe('Reason for assigning the role'), }, async ({ guildId, memberId, roleId, reason }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const member = await guild.members.fetch(memberId); const role = await guild.roles.fetch(roleId); if (!role) throw new Error('Role not found'); await member.roles.add(role, reason); return { memberId, roleId, roleName: role.name, memberName: member.displayName, message: 'Role assigned successfully', }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );
- src/index.ts:57-57 (registration)Top-level call to registerRoleTools(server), which includes registration of the 'assign_role' tool among other role tools.registerRoleTools(server);