Skip to main content
Glama

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
NameRequiredDescriptionDefault
guildIdYesThe ID of the server (guild)
memberIdYesThe ID of the member
roleIdYesThe ID of the role to assign
reasonNoReason for assigning the role

Implementation Reference

  • 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) }] }; }
  • 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'), },
  • 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);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/scarecr0w12/discord-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server