forked from QuarkSources/quarksources.github.io
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscord-bot.py
68 lines (60 loc) · 2 KB
/
discord-bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import discord
from discord.ext import commands
from discord_slash import cog_ext
from discord_slash.context import SlashContext
from discord_slash.model import SlashCommandOptionType
import os # Added to access environment variables
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
slash = cog_ext.cog_slash(command_name="assign_roles")
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
@bot.event
async def on_member_join(member):
# Send the role assignment message when a new member joins
await member.send("Welcome to the server! Please select your roles:")
await assign_roles_message(member)
@slash.options(
name="roles",
description="Select roles",
required=True,
option_type=SlashCommandOptionType.STRING,
choices=[
{
"name": "Role 1",
"value": "role1"
},
{
"name": "Role 2",
"value": "role2"
},
# Add more role choices as needed
]
)
@slash.target(["bot"])
async def assign_roles(ctx: SlashContext, roles: str):
member = ctx.author
role = discord.utils.get(ctx.guild.roles, name=roles)
if role:
await member.add_roles(role)
await ctx.send(f"Assigned {roles} role to {member.display_name}")
else:
await ctx.send(f"Role {roles} not found")
async def assign_roles_message(member):
# Send the role assignment message with the dropdown options
await member.send(
"Please select your roles:",
components=[
discord.ui.Select(
placeholder="Select roles...",
options=[
discord.SelectOption(label="Role 1", value="role1"),
discord.SelectOption(label="Role 2", value="role2"),
# Add more role options as needed
]
)
]
)
bot.run(os.getenv("DISCORD_TOKEN")) # Retrieve the bot token from the environment variable