pip install discord_components
import discord from discord.ext import commands from discord_components import Select, SelectOption class Menu(commands.Cog): def __init__(self, bot): self.bot = bot def setup(bot): bot.add_cog(Menu(bot))
@ commands.command() async def test(self, ctx): await ctx.send(components=[ Select(placeholder='Is python easy to learn?', options=[ SelectOption(label='Yes', value=1), SelectOption(label='Nope', value=0)])])
先等待使用者選取完選項
interaction = await self.bot.wait_for("select_option")
interaction = await self.bot.wait_for("select_option")
res = interaction.values[0]
if res == '1':
await ctx.send('very good!')
else:
await ctx.send('QQ')
加個 while True 迴圈,和一點小魔法即可
while True: interaction = await self.bot.wait_for("select_option") res = interaction.values[0] if res == '1': await ctx.send('very good!') else: await ctx.send('QQ') # 黑魔法 await interaction.respond(type=6)
記得導入時間相關的套件
from datetime import datetime
timestamp = datetime.timestamp(datetime.now()) await ctx.send(components=[ Select(placeholder='Is python easy to learn?', options=[ SelectOption(label='Yes', value=1), SelectOption(label='Nope', value=0)], custom_id=str(timestamp))])
每個選單只在這個選單對應時間戳記才會運作
interaction = await self.bot.wait_for("select_option", check=lambda inter: inter.custom_id == str(timestamp))
@ commands.command() async def test(self, ctx): timestamp = datetime.timestamp(datetime.now()) await ctx.send(components=[ Select(placeholder='Is python easy to learn?', options=[ SelectOption(label='Yes', value=1), SelectOption(label='Nope', value=0)], custom_id=str(timestamp))]) while True: interaction = await self.bot.wait_for("select_option", check=lambda inter: inter.custom_id == str(timestamp)) res = interaction.values[0] if res == '1': await ctx.send('very good!') else: await ctx.send('QQ') await interaction.respond(type=6)
role = ['第一個身分組的 ID', '第二個身分組的 ID']
async def add_roles(self, ctx, user, roleIDList): roleList = [] for i in roleIDList: role = ctx.guild.get_role(int(i)) roleList.append(role) await user.add_roles(*roleList)
async def remove_roles(self, ctx, user, roleIDList): roleList = [] for i in roleIDList: role = ctx.guild.get_role(int(i)) roleList.append(role) await user.remove_roles(*roleList)
await ctx.send('Get the roles!', components=[ Select(placeholder='Select your role!', options=[ SelectOption(label='roleA', value=role[0]), SelectOption(label='roleB', value=role[1])] , min_values=0, max_values=2, custom_id=str(timestamp))])
list(set(role).difference(interaction.values)
@ commands.command() async def rolelist(self, ctx): timestamp = datetime.timestamp(datetime.now()) await ctx.send('Get the roles!', components=[ Select(placeholder='Select your role!', options=[ SelectOption(label='roleA', value=role[0]), SelectOption(label='roleB', value=role[1])], min_values=0, max_values=2, custom_id=str(timestamp))]) while True: interaction = await self.bot.wait_for("select_option", check=lambda inter: inter.custom_id == str(timestamp)) await self.add_roles(ctx, interaction.user, interaction.values) await self.remove_roles(ctx, interaction.user, list(set(role).difference(interaction.values))) await interaction.respond(type=6)