คุณสามารถติดตั้งไลบรารีเวอร์ชัน PYPI ล่าสุดได้โดยทำ:
$ pip install reactionmenu
หรือเวอร์ชันการพัฒนา:
$ pip install git+https://github.com/Defxult/reactionmenu
จำเป็นต้องมีเจตนาขั้นต่ำ
bot = commands . Bot (..., intents = discord . Intents ( messages = True , guilds = True , reactions = True , members = True )) class reactionmenu.ReactionMenu(method: Union[Context, discord.Interaction], /, *, menu_type: MenuType, **kwargs)
ReactionMenu เป็นเมนูที่ใช้อิโมจิซึ่งเป็นอิโมจิกิลด์แบบกำหนดเองหรืออิโมจิปกติเพื่อควบคุมกระบวนการปนเปื้อน หากคุณไม่ได้มองหาคุณสมบัติแฟนซีและต้องการสิ่งที่เรียบง่ายนี่คือสิ่งที่ใช้
from reactionmenu import ReactionMenu , ReactionButtonไลบรารีนี้มาพร้อมกับวิธีการและตัวเลือกหลายอย่างเพื่อให้เมนูปฏิกิริยาไม่ลงรอยกันง่าย เมื่อคุณนำเข้าชั้นเรียนที่เหมาะสมคุณจะเริ่มต้นตัวสร้างอย่างเช่นนั้น:
menu = ReactionMenu ( method , menu_type = ReactionMenu . TypeEmbed )method ( Union[discord.ext.commands.Context, discord.Interaction] ) บริบทหรือวัตถุปฏิสัมพันธ์menu_type ( MenuType ) การกำหนดค่าของเมนูReactionMenu.TypeEmbed เมนูการปนเปื้อนแบบฝังปกติReactionMenu.TypeEmbedDynamic เมนูการปนเปื้อนที่ฝังอยู่กับข้อมูลแบบไดนามิกReactionMenu.TypeText , ข้อความเฉพาะเมนูการแบ่งหน้า| ชื่อ | พิมพ์ | ค่าเริ่มต้น | ใช้สำหรับ | ข้อมูล |
|---|---|---|---|---|
wrap_in_codeblock | str | None | ReactionMenu.TypeEmbedDynamic | ตัวระบุภาษา Discord Codeblock เพื่อห่อข้อมูลของคุณตัวอย่าง: ReactionMenu(ctx, ..., wrap_in_codeblock='py') |
custom_embed | discord.Embed | None | ReactionMenu.TypeEmbedDynamic | ฝังวัตถุที่จะใช้เมื่อเพิ่มข้อมูลด้วย ReactionMenu.add_row() ใช้เพื่อการจัดแต่งทรงผม |
delete_on_timeout | bool | False | All menu types | ลบเมนูเมื่อเวลาออกไป |
clear_reactions_after | bool | True | All menu types | ลบปฏิกิริยาทั้งหมดหลังจากเมนูหมดเวลา |
navigation_speed | str | ReactionMenu.NORMAL | All menu types | ตั้งค่าหากผู้ใช้ต้องการรอให้ปฏิกิริยาถูกลบออกโดยบอทก่อนที่จะ "หมุน" หน้า การตั้งค่าความเร็วเป็น ReactionMenu.FAST ทำให้มันไม่จำเป็นต้องรอ |
only_roles | List[discord.Role] | None | All menu types | หากตั้งค่าสมาชิกที่มีบทบาทใด ๆ ที่กำหนดเท่านั้นที่ได้รับอนุญาตให้ควบคุมเมนู เจ้าของเมนูสามารถควบคุมเมนูได้ตลอดเวลา |
timeout | Union[int, float, None] | 60.0 | All menu types | ตัวจับเวลาสำหรับเมื่อเมนูหมดเวลา None สามารถไม่มีการหมดเวลา |
show_page_director | bool | True | All menu types | แสดงที่ด้านล่างของแต่ละหน้าฝัง "หน้า 1/20" |
name | str | None | All menu types | ชื่อที่คุณสามารถตั้งค่าสำหรับเมนู |
style | str | "Page $/&" | All menu types | รูปแบบผู้กำกับหน้าแบบกำหนดเองที่คุณสามารถเลือกได้ "$" หมายถึงหน้าปัจจุบัน "&" หมายถึงจำนวนหน้าทั้งหมด ตัวอย่าง: ReactionMenu(ctx, ..., style='On $ out of &') |
all_can_click | bool | False | All menu types | ตั้งค่าหากทุกคนได้รับอนุญาตให้ควบคุมเมื่อหน้าถูก 'หมุน' เมื่อคลิกปุ่ม |
delete_interactions | bool | True | All menu types | ลบข้อความพรอมต์โดยบอทและข้อความตอบกลับโดยผู้ใช้เมื่อถามว่าหน้าใดที่พวกเขาต้องการไปเมื่อใช้ ReactionButton.Type.GO_TO_PAGE |
rows_requested | int | None | ReactionMenu.TypeEmbedDynamic | จำนวนข้อมูลต่อ ReactionMenu.add_row() ที่คุณต้องการนำไปใช้กับแต่ละหน้าฝัง |
remove_extra_emojis | bool | False | All menu types | หาก True อิโมจิทั้งหมด (ปฏิกิริยา) ที่เพิ่มเข้ามาในข้อความเมนูที่ไม่ได้เพิ่มลงในเมนูจะถูกลบออก |
ขึ้นอยู่กับ menu_type หน้าอาจเป็น str , discord.Embed หรือการรวมกันของ content และ files (ตัวอย่างด้านล่าง)
menu_type เป็น ReactionMenu.TypeEmbed ให้ใช้ EMBEDSmenu_type เป็น ReactionMenu.TypeText (เมนูข้อความเท่านั้น) หรือ ReactionMenu.TypeEmbedDynamic (ฝังเมนูเท่านั้น) ให้ใช้สตริงReactionMenu.add_page(embed: discord.Embed=MISSING, content: Optional[str]=None, files: Optional[Sequence[discord.File]]=None)ReactionMenu.add_pages(pages: Sequence[Union[discord.Embed, str]])ReactionMenu.add_row(data: str)ReactionMenu.remove_all_pages()ReactionMenu.clear_all_row_data()ReactionMenu.remove_page(page_number: int)ReactionMenu.set_main_pages(*embeds: Embed)ReactionMenu.set_last_pages(*embeds: Embed) # ReactionMenu.TypeEmbed
menu = ReactionMenu ( method , menu_type = ReactionMenu . TypeEmbed )
menu . add_page ( summer_embed )
menu . add_page ( winter_embed )
# ReactionMenu.TypeText
menu = ReactionMenu ( method , menu_type = ReactionMenu . TypeText )
menu . add_page ( content = 'Its so hot!' )
menu . add_page ( content = 'Its so cold!' ) เมนู TypeText เป็นเมนูการแบ่งหน้าด้วยข้อความ ไม่มีการฝังอยู่ในกระบวนการปนเปื้อนจะใช้ข้อความธรรมดาเท่านั้น
ด้วย v3.1.0+ คุณสามารถ paginate ด้วยมากกว่าแค่การฝังหรือข้อความ คุณสามารถรวมข้อความฝังและไฟล์ แต่ขึ้นอยู่กับ menu_type การรวมกันอาจถูก จำกัด นี่คือตัวอย่างของเมนูที่มี menu_type ของ TypeEmbed ที่ซ้อนกัน
# You can use regular commands as well
@ bot . tree . command ( description = "These are stacked pages" , guild = discord . Object ( id = ...))
async def stacked ( interaction : discord . Interaction ):
menu = ReactionMenu ( interaction , menu_type = ReactionMenu . TypeEmbed )
menu . add_page ( discord . Embed ( title = "My Embed" ), content = "This content is stacked on top of a file" , files = [ discord . File ( "stacked.py" )])
menu . add_page ( discord . Embed ( title = "Hey Wumpos, can you say hi to the person reading this? ?" ))
menu . add_page ( discord . Embed ( title = "Hi, I'm Wumpos!" ), files = [ discord . File ( "wumpos.gif" )])
menu . add_button ( ReactionButton . back ())
menu . add_button ( ReactionButton . next ())
await menu . start () เนื่องจาก menu_type เป็น TypeEmbed จึงต้องมีการฝังในแต่ละหน้าเสมอ หาก menu_type เป็น TypeText ไม่อนุญาตให้ฝังอยู่และคุณจะถูก จำกัด ให้ใช้พารามิเตอร์ files เท่านั้น
เมนูแบบไดนามิกจะใช้เมื่อคุณไม่ทราบว่าจะใช้ข้อมูลมากแค่ไหนกับเมนู ตัวอย่างเช่นหากคุณจะขอข้อมูลจากฐานข้อมูลข้อมูลนั้นสามารถเปลี่ยนแปลงได้เสมอ คุณสอบถามบางสิ่งบางอย่างและคุณอาจได้รับ 1,500 ผลลัพธ์กลับมาและอาจมีเพียง 800 เท่านั้นเมนูไดนามิกชิ้นข้อมูลทั้งหมดนี้เข้าด้วยกันสำหรับคุณและเพิ่มลงในหน้าฝังด้วยข้อมูล ReactionMenu.add_row() ใช้งานได้ดีที่สุดในบางประเภทของ Iterable ที่ทุกอย่างสามารถวนซ้ำได้ แต่เพิ่มปริมาณข้อมูลที่คุณต้องการลงในหน้าเมนูเท่านั้น
หมายเหตุ: ในเมนูแบบไดนามิกข้อมูลที่เพิ่มทั้งหมดจะถูกวางไว้ในส่วนคำอธิบายของการฝัง หากคุณเลือกที่จะใช้
custom_embedข้อความทั้งหมดในคำอธิบายจะถูกแทนที่ด้วยข้อมูลที่คุณเพิ่ม
ReactionMenu.add_row(data: str)ReactionMenu.clear_all_row_data()ReactionMenu.set_main_pages(*embeds: Embed)ReactionMenu.set_last_pages(*embeds: Embed)rows_requested - จำนวนแถวที่คุณต้องการในแต่ละหน้าฝังก่อนที่จะสร้างหน้าใหม่ReactionMenu(..., rows_requested=5)custom_embed - การฝังที่คุณสร้างขึ้นเพื่อใช้เป็นหน้าฝัง ใช้สำหรับความงามเมนูของคุณReactionMenu(..., custom_embed=red_embed)wrap_in_codeblock - ตัวระบุภาษาเมื่อห่อข้อมูลของคุณใน codeblock discordReactionMenu(..., wrap_in_codeblock='py') menu = ReactionMenu ( ctx , menu_type = ReactionMenu . TypeEmbedDynamic , rows_requested = 5 )
for data in database . request ( 'SELECT * FROM customers' ):
menu . add_row ( data ) คุณสามารถลบข้อมูลทั้งหมดที่คุณเพิ่มลงในเมนูโดยใช้ menu.clear_all_row_data()
เมื่อใช้เมนูแบบไดนามิกหน้าเว็บที่คุณเห็นเพียงอย่างเดียวที่คุณเห็นมาจากข้อมูลที่คุณเพิ่ม แต่ถ้าคุณต้องการแสดงหน้าอื่น ๆ นอกเหนือจากข้อมูลคุณสามารถใช้ Methods ReactionMenu.set_main_pages() และ ReactionMenu.set_last_pages() การตั้งค่าหน้าหลักการฝังที่คุณตั้งค่าจะเป็นตัวฝังตัวแรกที่แสดงเมื่อเมนูเริ่มต้น การตั้งค่าหน้าสุดท้ายคือการฝังสุดท้ายที่แสดง
menu . set_main_pages ( welcome_embed , announcement_embed )
for data in get_information ():
menu . add_row ( data )
menu . set_last_pages ( additional_info_embed )
# NOTE: setting main/last pages can be set in any orderปุ่ม/ประเภทปุ่มจะใช้เมื่อคุณต้องการเพิ่มปฏิกิริยากับเมนูที่ทำฟังก์ชั่นบางอย่าง ปุ่มและประเภทปุ่มทำงานร่วมกันเพื่อให้เกิดการกระทำที่ต้องการ
class reactionmenu.ReactionButton(*, emoji: str, linked_to: ButtonType, **kwargs)
emoji ( str ) อิโมจิที่คุณต้องการใช้เป็นปฏิกิริยาlinked_to ( ReactionButton.Type ) เมื่อกดปฏิกิริยานี่คือสิ่งที่กำหนดว่าจะทำอย่างไร| ชื่อ | พิมพ์ | ค่าเริ่มต้น | ใช้สำหรับ |
|---|---|---|---|
embed | discord.Embed | None | เมื่อกดปฏิกิริยาให้ไปที่การฝังที่ระบุ |
name | str | None | ชื่อของปุ่ม |
details | ข้อมูลด้านล่าง | None | กำหนดฟังก์ชั่นและเป็นข้อโต้แย้งที่จะเรียกเมื่อมี ReactionButton กับ ReactionButton.Type.CALLER ถูกกด |
event | ReactionButton.Event | None | กำหนดว่าควรลบปุ่มเมื่อใดขึ้นอยู่กับจำนวนครั้งที่กด |
skip | ReactionButton.Skip | None | ตั้งค่าการกระทำและจำนวนหน้าเพื่อข้ามเมื่อใช้ linked_to ของ ReactionButton.Type.SKIP ตัวอย่างเช่นการใช้ประเภทปุ่มนี้การตั้งค่าการกระทำเป็น "+" และจำนวนเงิน 3 ถ้าคุณอยู่ใน "หน้า 1/20" การกดปุ่มนั้นจะนำคุณไปสู่ "หน้า 4/20" |
| คุณสมบัติ | ประเภทกลับ | ข้อมูล |
|---|---|---|
clicked_by | Set[discord.Member] | สมาชิกที่คลิกปุ่ม |
total_clicks | int | จำนวนคลิกจากปุ่ม |
last_clicked | Optional[datetime.datetime] | เวลาใน UTC สำหรับเมื่อมีการคลิกปุ่มล่าสุด |
menu | Optional[ReactionMenu] | เมนูปุ่มติดอยู่กับ |
ReactionMenu.add_button(button: ReactionButton)ReactionMenu.remove_all_buttons()ReactionMenu.remove_button(button: ReactionButton)ReactionMenu.get_button(identity: Union[str, int], *, search_by='name')ReactionButton.set_caller_details(func: Callable[..., None], *args, **kwargs)| พิมพ์ | ข้อมูล |
|---|---|
ReactionButton.Type.NEXT_PAGE | ไปที่หน้าถัดไปในเซสชันเมนู |
ReactionButton.Type.PREVIOUS_PAGE | ไปที่หน้าก่อนหน้าในเซสชันเมนู |
ReactionButton.Type.GO_TO_FIRST_PAGE | ไปที่หน้าแรกในเซสชันเมนู |
ReactionButton.Type.GO_TO_LAST_PAGE | ไปที่หน้าสุดท้ายในเซสชันเมนู |
ReactionButton.Type.GO_TO_PAGE | แจ้งให้คุณพิมพ์ในหน้าเว็บที่คุณต้องการไป |
ReactionButton.Type.END_SESSION | หยุดเซสชันและลบข้อความเมนู |
ReactionButton.Type.CUSTOM_EMBED | ใช้แยกต่างหากจากปุ่มนำทาง เมื่อกดแล้วไปที่ Embed ที่ระบุ |
ReactionButton.Type.CALLER | ใช้เมื่อระบุฟังก์ชั่นการโทรและเป็นอาร์กิวเมนต์เมื่อกดปุ่ม |
ReactionButton.Type.SKIP | ใช้ในการตัดผ่านหลายหน้าในปุ่มเดียวกด |
คุณสามารถเพิ่มปุ่ม (ปฏิกิริยา) ลงในเมนูโดยใช้ ReactionButton ด้านล่างนี้เป็นตัวอย่างเกี่ยวกับวิธีการใช้ ButtonType แต่ละครั้ง
หมายเหตุ: ReactionButtons กับ
ReactionButton.Type.CALLERแตกต่างกันเล็กน้อยดังนั้นจึงมีส่วนเฉพาะที่อธิบายว่าพวกเขาทำงานอย่างไรและจะนำไปใช้อย่างไรด้านล่างด้านล่าง
menu = ReactionMenu (...)
# first and last pages
fpb = ReactionButton ( emoji = '⏪' , linked_to = ReactionButton . Type . GO_TO_FIRST_PAGE )
lpb = ReactionButton ( emoji = '⏩' , linked_to = ReactionButton . Type . GO_TO_LAST_PAGE )
# go to page
gtpb = ReactionButton ( emoji = '?' , linked_to = ReactionButton . Type . GO_TO_PAGE )
# end session
esb = ReactionButton ( emoji = '⏹️' , linked_to = ReactionButton . Type . END_SESSION )
# custom embed
ceb = ReactionButton ( emoji = '?' , linked_to = ReactionButton . Type . CUSTOM_EMBED , embed = discord . Embed ( title = 'Hello' ))
# skip button
sb = ReactionButton ( emoji = '5️⃣' , linked_to = ReactionButton . Type . SKIP , skip = ReactionButton . Skip ( action = '+' , amount = 5 ))
menu . add_button ( fpb )
... ลบปุ่มทั้งหมดด้วย menu.remove_all_buttons() นอกจากนี้คุณยังสามารถลบปุ่มแต่ละตัวโดยใช้ชื่อหากคุณตั้งค่าหรือวัตถุปุ่มเองด้วย menu.remove_button()
ReactionButton.Type.CALLER ปุ่มใช้เพื่อใช้การทำงานของคุณเองในเมนู บางทีคุณอาจต้องการเพิ่มปุ่มที่สร้างช่องข้อความส่งข้อความหรือเพิ่มบางอย่างลงในฐานข้อมูลไม่ว่าจะเป็นอะไรก็ตาม เพื่อที่จะทำงานกับ ReactionButton.Type.CALLER ให้ใช้วิธีการเรียนด้านล่าง
ReactionButton.set_caller_details(func: Callable[..., None], *args, **kwargs) วิธีการคลาสนี้ใช้ในการตั้งค่าฟังก์ชั่นและเป็นอาร์กิวเมนต์ที่เรียกในภายหลังเมื่อกดปุ่ม ตัวสร้าง ReactionButton มี details KWARG และนั่นคือสิ่งที่คุณจะใช้กับ .set_caller_details() เพื่อกำหนดค่าที่ต้องการ ตัวอย่างบางส่วนอยู่ด้านล่างเกี่ยวกับวิธีการใช้ ReactionButton.Type.CALLER อย่างถูกต้อง
@ bot . command ()
async def user ( ctx , name , * , message ):
await ctx . send ( f"Hi { name } ! { message } . We're glad you're here!" )
def car ( year , make , model ):
print ( f"I have a { year } { make } { model } " )
ub = ReactionButton ( emoji = '' , linked_to = ReactionButton . Type . CALLER , details = ReactionButton . set_caller_details ( user , ctx , 'Defxult' , message = 'Welcome to the server' ))
cb = ReactionButton ( emoji = '?' , linked_to = ReactionButton . Type . CALLER , details = ReactionButton . set_caller_details ( car , 2021 , 'Ford' , 'Mustang' ))หมายเหตุ: ฟังก์ชั่นที่คุณส่งผ่านไม่ควรส่งคืนอะไร ฟังก์ชั่นการโทรด้วย
ReactionButton.Type.CALLERไม่ได้จัดเก็บหรือจัดการสิ่งใดที่ส่งคืนโดยฟังก์ชั่นนั้น
คลาส ReactionButton มาพร้อมกับวิธีการตั้งค่าโรงงาน (วิธีการคลาส) ที่ส่งคืน ReactionButton ด้วยพารามิเตอร์ที่ตั้งค่าตาม linked_to ของพวกเขา
ReactionButton.back()emoji : "linked_to : ReactionButton.Type.PREVIOUS_PAGEReactionButton.next()emoji : "linked_to : ReactionButton.Type.NEXT_PAGEReactionButton.go_to_first_page()emoji : "⏪"linked_to : ReactionButton.Type.GO_TO_FIRST_PAGEReactionButton.go_to_last_page()emoji : "⏩"linked_to : ReactionButton.Type.GO_TO_LAST_PAGEReactionButton.go_to_page()emoji : "?"linked_to : ReactionButton.Type.GO_TO_PAGEReactionButton.end_session()emoji : "⏹"linked_to : ReactionButton.Type.END_SESSIONReactionButton.all()list ReactionButton ในลำดับต่อไปนี้.go_to_first_page() .back() .next() .go_to_last_page() .go_to_page() .end_session()ReactionButton.generate_skip(emoji: str, action: str, amount: int)emoji : <emoji>linked_to : ReactionButton.Type.SKIPskip : ReactionButton.Skip(<action>, <amount>)หากคุณต้องการคุณสามารถ จำกัด จำนวนเมนูปฏิกิริยาที่สามารถใช้งานได้ในเวลาเดียวกัน ต่อ "กิลด์", "สมาชิก" หรือ "ช่อง"
ReactionMenu.set_sessions_limit(limit: int, per='guild', message='Too many active menus. Wait for other menus to be finished.')ReactionMenu.remove_limit()ตัวอย่าง:
@ bot . command ()
async def limit ( ctx ):
ReactionMenu . set_sessions_limit ( 3 , per = 'member' , message = 'Sessions are limited to 3 per member' )ด้วยตัวอย่างข้างต้นมีเมนูเพียง 3 เมนูเท่านั้นที่สามารถใช้งานได้พร้อมกันสำหรับสมาชิกแต่ละคนและหากพวกเขาพยายามสร้างเพิ่มเติมก่อนที่เมนูอื่นจะเสร็จสิ้นพวกเขาจะได้รับข้อความแสดงข้อผิดพลาดว่า "เซสชันถูก จำกัด ไว้ที่ 3 ต่อสมาชิก"
คุณสามารถตั้ง ReactionButton ที่จะถูกลบออกเมื่อมีการกดจำนวนครั้งที่กำหนด
class ReactionButton.Event(event_type: str, value: int)
event_type ( str ) การกระทำที่จะดำเนินการ ตัวเลือกที่มีอยู่เท่านั้นคือ "ลบ"value ( int ) จำนวนเงินที่ตั้งไว้สำหรับเหตุการณ์ที่ระบุ ต้องเป็น> = 1. ถ้าค่าคือ <= 0 มันจะถูกตั้งค่าเป็น 1 โดยปริยายเป็น 1ตัวอย่าง:
menu = ReactionMenu ( ctx , ...)
# remove a button after 10 clicks
button = ReactionButton (..., event = ReactionButton . Event ( 'remove' , 10 ))
menu . add_button ( button )หมายเหตุ: ไม่เหมาะสำหรับปุ่มที่มี
linked_toของReactionButton.Type.END_SESSION
รีเลย์เมนูเป็นฟังก์ชั่นที่เรียกว่าทุกเวลาปุ่มที่แยกออกจากเมนูจะถูกกด มันถือเป็นส่วนขยายของ ReactionButton ที่มี linked_to ของ ReactionButton.Type.CALLER ซึ่งแตกต่างจากปุ่มผู้โทรซึ่งไม่มีรายละเอียดเกี่ยวกับการโต้ตอบในเมนูรีเลย์ทำ
ReactionMenu.set_relay(func: Callable[[NamedTuple], None], *, only: Optional[List[ReactionButton]]=None)ReactionMenu.remove_relay() เมื่อสร้างฟังก์ชั่นสำหรับรีเลย์ของคุณฟังก์ชั่นนั้นจะต้องมีอาร์กิวเมนต์ตำแหน่งเดียว เมื่อกดปุ่มวัตถุ RelayPayload (tuple ที่มีชื่อ) จะถูกส่งผ่านไปยังฟังก์ชั่นนั้น แอตทริบิวต์ของ RelayPayload คือ:
member ( discord.Member ) บุคคลที่กดปุ่มbutton ( ReactionButton ) ปุ่มที่ถูกกดตัวอย่าง:
async def enter_giveaway ( payload ):
member = payload . member
channel = payload . button . menu . message . channel
await channel . send ( f" { member . mention } , you've entered the giveaway!" )
menu = ReactionMenu ( ctx , ...)
menu . set_relay ( enter_giveaway ) วิธี set_relay มาพร้อมกับพารามิเตอร์ only หากพารามิเตอร์นั้น None ปุ่มทั้งหมดที่กดจะถูกถ่ายทอด คุณสามารถระบุ list ปุ่มไปยังพารามิเตอร์นั้นได้ดังนั้นจะมีเพียงปุ่มกดจากปุ่มที่ระบุเหล่านั้นเท่านั้นที่จะถูกถ่ายทอด
def example ( payload ):
...
menu = ReactionMenu ( ctx , ...)
back_button = ReactionButton . back ()
next_button = ReactionButton . next ()
menu . set_relay ( example , only = [ back_button ])await ReactionMenu.start(*, send_to=None, reply=False)await ReactionMenu.stop(*, delete_menu_message=False, clear_reactions=False) เมื่อเริ่มต้นเมนูคุณมีตัวเลือกในการส่งเมนูไปยังช่องทางหนึ่ง พารามิเตอร์ send_to เป็นช่องที่คุณต้องการส่งเมนูไป คุณสามารถตั้ง send_to เป็นชื่อช่อง ( str ), ID ช่อง ( int ) หรือวัตถุช่อง ( discord.TextChannel / discord.Thread ) ตัวอย่าง:
menu = ReactionMenu (...)
# channel name
await menu . start ( send_to = 'bot-commands' )
# channel ID
await menu . start ( send_to = 1234567890123456 )
# channel object
channel = guild . get_channel ( 1234567890123456 )
await menu . start ( send_to = channel )
# there's no need to specify send_to unless you want the menu to be sent to a different channel
# from the one you're sending the initial message/using the command in. the menu can be started
# in the current channel by omitting the send_to parameter
await menu . start ()หมายเหตุ:
send_toไม่ถูกต้องหากเมนูเริ่มต้นใน DM's
นี่คือการใช้งานพื้นฐานของ ReactionMenu ที่คุณสามารถคัดลอกและวางเพื่อการสาธิตอย่างรวดเร็ว
import asyncio
import discord
from discord . ext import commands
from reactionmenu import ReactionMenu , ReactionButton
bot = commands . Bot ( command_prefix = '!' , intents = discord . Intents . all ())
async def start_bot ():
async with bot :
await bot . start ( '...' )
@ bot . command ()
async def example ( ctx ):
menu = ReactionMenu ( ctx , menu_type = ReactionMenu . TypeEmbed )
for member in ctx . guild . members :
if member . avatar :
embed = discord . Embed ( description = f'Joined { member . joined_at . strftime ( "%b. %d, %Y" ) } ' )
embed . set_author ( name = member . name , icon_url = member . avatar . url )
menu . add_page ( embed )
menu . add_button ( ReactionButton . back ())
menu . add_button ( ReactionButton . next ())
menu . add_button ( ReactionButton . end_session ())
await menu . start ()
asyncio . run ( start_bot ()) class reactionmenu.ViewMenu(method: Union[Context, discord.Interaction], /, *, menu_type: MenuType, **kwargs)
ViewMenu เป็นเมนูที่ใช้คุณสมบัติปุ่ม Discords ด้วยปุ่มคุณสามารถเปิดใช้งานและปิดใช้งานได้ตั้งค่าสีที่แน่นอนสำหรับพวกเขาด้วยอิโมจิมีปุ่มที่ส่งข้อความที่ซ่อนอยู่และเพิ่มไฮเปอร์ลิงก์ ห้องสมุดนี้มีฟังก์ชันการทำงานที่กว้างขึ้นเช่นผู้กดปุ่มมีกี่ครั้งที่กดและอื่น ๆ มันใช้มุมมอง ( discord.ui.View ) เพื่อใช้ฟังก์ชันการทำงานของปุ่ม แต่ใช้วิธีการบางอย่างของตัวเองเพื่อให้เมนูการแบ่งหน้าปุ่มง่าย
from reactionmenu import ViewMenu , ViewButtonmethod ( Union[discord.ext.commands.Context, discord.Interaction] ) บริบทหรือวัตถุปฏิสัมพันธ์menu_type ( MenuType ) การกำหนดค่าของเมนูViewMenu.TypeEmbed เมนูการปนเปื้อนแบบฝังปกติViewMenu.TypeEmbedDynamic , เมนูการปนเปื้อนแบบฝังพร้อมข้อมูลแบบไดนามิกViewMenu.TypeText , ข้อความเฉพาะเมนูการแบ่งหน้า| ชื่อ | พิมพ์ | ค่าเริ่มต้น | ใช้สำหรับ | ข้อมูล |
|---|---|---|---|---|
wrap_in_codeblock | str | None | ViewMenu.TypeEmbedDynamic | ตัวระบุภาษา codeblock discord เพื่อห่อข้อมูลของคุณในตัวอย่าง: ViewMenu(ctx, ..., wrap_in_codeblock='py') |
custom_embed | discord.Embed | None | ViewMenu.TypeEmbedDynamic | ฝังวัตถุที่จะใช้เมื่อเพิ่มข้อมูลด้วย ViewMenu.add_row() ใช้เพื่อการจัดแต่งทรงผม |
delete_on_timeout | bool | False | All menu types | ลบเมนูเมื่อเวลาออกไป |
disable_items_on_timeout | bool | True | All menu types | ปิดการใช้งานรายการในเมนูเมื่อเมนูหมดเวลา |
remove_items_on_timeout | bool | False | All menu types | ลบรายการในเมนูเมื่อเมนูหมดเวลา |
only_roles | List[discord.Role] | None | All menu types | หากตั้งค่าสมาชิกที่มีบทบาทใด ๆ ที่กำหนดเท่านั้นที่ได้รับอนุญาตให้ควบคุมเมนู เจ้าของเมนูสามารถควบคุมเมนูได้ตลอดเวลา |
timeout | Union[int, float, None] | 60.0 | All menu types | ตัวจับเวลาสำหรับเมื่อเมนูหมดเวลา None สามารถไม่มีการหมดเวลา |
show_page_director | bool | True | All menu types | แสดงที่ด้านล่างของแต่ละหน้าฝัง "หน้า 1/20" |
name | str | None | All menu types | ชื่อที่คุณสามารถตั้งค่าสำหรับเมนู |
style | str | "Page $/&" | All menu types | รูปแบบผู้กำกับหน้าแบบกำหนดเองที่คุณสามารถเลือกได้ "$" หมายถึงหน้าปัจจุบัน "&" หมายถึงจำนวนหน้าทั้งหมด ตัวอย่าง: ViewMenu(ctx, ..., style='On $ out of &') |
all_can_click | bool | False | All menu types | ตั้งค่าหากทุกคนได้รับอนุญาตให้ควบคุมเมื่อหน้าถูก 'หมุน' เมื่อคลิกปุ่ม |
delete_interactions | bool | True | All menu types | ลบข้อความพรอมต์โดยบอทและข้อความตอบกลับโดยผู้ใช้เมื่อถามว่าหน้าใดที่พวกเขาต้องการไปเมื่อใช้ ViewButton.ID_GO_TO_PAGE |
rows_requested | int | None | ViewMenu.TypeEmbedDynamic | จำนวนข้อมูลต่อ ViewMenu.add_row() คุณต้องการนำไปใช้กับแต่ละหน้าฝัง |
ขึ้นอยู่กับ menu_type หน้าอาจเป็น str , discord.Embed หรือการรวมกันของ content หรือ files (ตัวอย่างด้านล่าง)
menu_type เป็น ViewMenu.TypeEmbed ให้ใช้ EMBEDSmenu_type เป็น ViewMenu.TypeText (เมนูข้อความเท่านั้น) หรือ ViewMenu.TypeEmbedDynamic (ฝังเมนูเท่านั้น) ให้ใช้สตริงViewMenu.add_page(embed: discord.Embed=MISSING, content: Optional[str]=None, files: Optional[Sequence[discord.File]]=None)ViewMenu.add_pages(pages: Sequence[Union[discord.Embed, str]])ViewMenu.add_row(data: str)ViewMenu.remove_all_pages()ViewMenu.clear_all_row_data()ViewMenu.remove_page(page_number: int)ViewMenu.set_main_pages(*embeds: Embed)ViewMenu.set_last_pages(*embeds: Embed) # ViewMenu.TypeEmbed
menu = ViewMenu ( method , menu_type = ViewMenu . TypeEmbed )
menu . add_page ( summer_embed )
menu . add_page ( winter_embed )
# ViewMenu.TypeText
menu = ViewMenu ( method , menu_type = ViewMenu . TypeText )
menu . add_page ( content = 'Its so hot!' )
menu . add_page ( content = 'Its so cold!' ) เมนู TypeText เป็นเมนูการแบ่งหน้าด้วยข้อความ ไม่มีการฝังอยู่ในกระบวนการปนเปื้อนจะใช้ข้อความธรรมดาเท่านั้น
ด้วย v3.1.0+ คุณสามารถ paginate ด้วยมากกว่าแค่การฝังหรือข้อความ คุณสามารถรวมข้อความฝังและไฟล์ แต่ขึ้นอยู่กับ menu_type การรวมกันอาจถูก จำกัด นี่คือตัวอย่างของเมนูที่มี menu_type ของ TypeEmbed ที่ซ้อนกัน
# You can use regular commands as well
@ bot . tree . command ( description = "These are stacked pages" , guild = discord . Object ( id = ...))
async def stacked ( interaction : discord . Interaction ):
menu = ViewMenu ( interaction , menu_type = ViewMenu . TypeEmbed )
menu . add_page ( discord . Embed ( title = "My Embed" ), content = "This content is stacked on top of a file" , files = [ discord . File ( "stacked.py" )])
menu . add_page ( discord . Embed ( title = "Hey Wumpos, can you say hi to the person reading this? ?" ))
menu . add_page ( discord . Embed ( title = "Hi, I'm Wumpos!" ), files = [ discord . File ( "wumpos.gif" )])
menu . add_button ( ViewButton . back ())
menu . add_button ( ViewButton . next ())
await menu . start () เนื่องจาก menu_type เป็น TypeEmbed จึงต้องมีการฝังในแต่ละหน้าเสมอ หาก menu_type เป็น TypeText ไม่อนุญาตให้ฝังอยู่และคุณจะถูก จำกัด ให้ใช้พารามิเตอร์ files เท่านั้น
เมนูแบบไดนามิกจะใช้เมื่อคุณไม่ทราบว่าจะใช้ข้อมูลมากแค่ไหนกับเมนู ตัวอย่างเช่นหากคุณจะขอข้อมูลจากฐานข้อมูลข้อมูลนั้นสามารถเปลี่ยนแปลงได้เสมอ คุณสอบถามบางสิ่งบางอย่างและคุณอาจได้รับ 1,500 ผลลัพธ์กลับมาและอาจมีเพียง 800 เท่านั้นเมนูไดนามิกชิ้นข้อมูลทั้งหมดนี้เข้าด้วยกันสำหรับคุณและเพิ่มลงในหน้าฝังด้วยข้อมูล ViewMenu.add_row() ใช้งานได้ดีที่สุดในบางประเภทของ Iterable ที่ทุกอย่างสามารถวนซ้ำได้ แต่เพิ่มปริมาณข้อมูลที่คุณต้องการในหน้าเมนูเท่านั้น
หมายเหตุ: ในเมนูแบบไดนามิกข้อมูลที่เพิ่มทั้งหมดจะถูกวางไว้ในส่วนคำอธิบายของการฝัง หากคุณเลือกที่จะใช้
custom_embedข้อความทั้งหมดในคำอธิบายจะถูกแทนที่ด้วยข้อมูลที่คุณเพิ่ม
ViewMenu.add_row(data: str)ViewMenu.clear_all_row_data()ViewMenu.set_main_pages(*embeds: Embed)ViewMenu.set_last_pages(*embeds: Embed)rows_requested - จำนวนแถวที่คุณต้องการในแต่ละหน้าฝังก่อนที่จะสร้างหน้าใหม่ViewMenu(..., rows_requested=5)custom_embed - การฝังที่คุณสร้างขึ้นเพื่อใช้เป็นหน้าฝัง ใช้สำหรับความงามเมนูของคุณViewMenu(..., custom_embed=red_embed)wrap_in_codeblock - ตัวระบุภาษาเมื่อห่อข้อมูลของคุณใน codeblock discordViewMenu(..., wrap_in_codeblock='py') menu = ViewMenu ( ctx , menu_type = ViewMenu . TypeEmbedDynamic , rows_requested = 5 )
for data in database . request ( 'SELECT * FROM customers' ):
menu . add_row ( data ) คุณสามารถลบข้อมูลทั้งหมดที่คุณเพิ่มลงในเมนูโดยใช้ menu.clear_all_row_data()
เมื่อใช้เมนูแบบไดนามิกหน้าเว็บที่คุณเห็นเพียงอย่างเดียวที่คุณเห็นมาจากข้อมูลที่คุณเพิ่ม แต่ถ้าคุณต้องการแสดงหน้าอื่น ๆ นอกเหนือจากข้อมูลคุณสามารถใช้วิธีการ ViewMenu.set_main_pages() และ ViewMenu.set_last_pages() การตั้งค่าหน้าหลักการฝังที่คุณตั้งค่าจะเป็นตัวฝังตัวแรกที่แสดงเมื่อเมนูเริ่มต้น การตั้งค่าหน้าสุดท้ายคือการฝังสุดท้ายที่แสดง
menu . set_main_pages ( welcome_embed , announcement_embed )
for data in get_information ():
menu . add_row ( data )
menu . set_last_pages ( additional_info_embed )
# NOTE: setting main/last pages can be set in any orderปุ่มคือสิ่งที่คุณใช้ในการโต้ตอบกับเมนู พวกเขาดูสะอาดกว่าปฏิกิริยาที่ไม่ได้รับการตอบโต้น้อยลงมีปัญหาขีด จำกัด น้อยลงและเสนอเพิ่มเติมในแง่ของการโต้ตอบ เปิดใช้งานและปิดการใช้งานปุ่มใช้การเชื่อมโยงหลายมิติของ Markdown ในข้อความและแม้แต่ส่งข้อความที่ซ่อนอยู่
ViewMenu.add_button(button: ViewButton)ViewMenu.disable_all_buttons()ViewMenu.disable_button(button: ViewButton)ViewMenu.enable_all_buttons()ViewMenu.enable_button(button: ViewButton)ViewMenu.get_button(identity: str, *, search_by='label')ViewMenu.remove_all_buttons()ViewMenu.remove_button(button: ViewButton)await ViewMenu.refresh_menu_items() class reactionmenu.ViewButton(*, style=discord.ButtonStyle.secondary, label=None, disabled=False, custom_id=None, url=None, emoji=None, followup=None, event=None, **kwargs)
ViewButton เป็นคลาสที่แสดงถึงปุ่ม Discord มันเป็นคลาสย่อยของ discord.ui.Button
ต่อไปนี้เป็นกฎที่กำหนดโดย Discord for Buttons:
custom_id และไม่สามารถมี urlurl และไม่สามารถมี custom_idstyle ( discord.ButtonStyle ) ปุ่มสไตล์label ( str ) ข้อความบนปุ่มcustom_id ( str ) ID เพื่อพิจารณาว่าการกระทำนั้นควรใช้ปุ่มใด รหัสที่มีอยู่:ViewButton.ID_NEXT_PAGEViewButton.ID_PREVIOUS_PAGEViewButton.ID_GO_TO_FIRST_PAGEViewButton.ID_GO_TO_LAST_PAGEViewButton.ID_GO_TO_PAGEViewButton.ID_END_SESSIONViewButton.ID_CALLERViewButton.ID_SEND_MESSAGEViewButton.ID_CUSTOM_EMBEDViewButton.ID_SKIPemoji ( Union[str, discord.PartialEmoji] ) emoji ใช้สำหรับปุ่มViewButton(..., emoji='?')ViewButton(..., emoji='<:miscTwitter:705423192818450453>')ViewButton(..., emoji='U000027a1')ViewButton(..., emoji='N{winking face}')url ( str ) url สำหรับปุ่มที่มีสไตล์ discord.ButtonStyle.linkdisabled ( bool ) หากปุ่มควรปิดใช้งานfollowup ( ViewButton.Followup ) ข้อความที่ส่งหลังจากกดปุ่ม ใช้ได้เฉพาะกับปุ่มที่มี custom_id ของ ViewButton.ID_CALLER หรือ ViewButton.ID_SEND_MESSAGE ViewButton.Followup เป็นคลาสที่มีพารามิเตอร์คล้ายกับ discord.abc.Messageable.send() และใช้เพื่อควบคุมว่าข้อความจะไม่ชั่วคราวมีไฟล์ฝัง TTs ฯลฯ ...event ( ViewButton.Event ) ตั้งปุ่มที่จะปิดใช้งานหรือลบออกเมื่อกดจำนวนครั้งที่กำหนด | ชื่อ | พิมพ์ | ค่าเริ่มต้น | ใช้สำหรับ |
|---|---|---|---|
name | str | None | ชื่อของปุ่ม |
skip | ViewButton.Skip | None | ตั้งค่าการกระทำและจำนวนหน้าเพื่อข้ามเมื่อใช้ custom_id ของ ViewButton.ID_SKIP ตัวอย่างเช่นการตั้งค่าการกระทำเป็น "+" และจำนวนเงิน 3 ถ้าคุณอยู่ใน "หน้า 1/20" การกดปุ่มนั้นจะนำคุณไปที่ "หน้า 4/20" |
persist | bool | False | ป้องกันไม่ให้ปุ่มลิงค์ถูกปิดใช้งาน/ลบเมื่อเมนูหมดเวลาหรือหยุดลงเพื่อให้สามารถคลิกได้ |
| คุณสมบัติ | ประเภทกลับ | ข้อมูล |
|---|---|---|
clicked_by | Set[discord.Member] | สมาชิกที่คลิกปุ่ม |
total_clicks | int | จำนวนคลิกจากปุ่ม |
last_clicked | Optional[datetime.datetime] | เวลาใน UTC สำหรับเมื่อมีการคลิกปุ่มล่าสุด |
menu | Optional[ViewMenu] | เมนูปุ่มติดอยู่กับ |
from reactionmenu import ViewMenu , ViewButton
menu = ViewMenu ( ctx , menu_type = ViewMenu . TypeEmbed )
# Link button
link_button = ViewButton ( style = discord . ButtonStyle . link , emoji = '?' , label = 'Link to Google' , url = 'https://google.com' )
menu . add_button ( link_button )
# Skip button
skip = ViewButton ( style = discord . ButtonStyle . primary , label = '+5' , custom_id = ViewButton . ID_SKIP , skip = ViewButton . Skip ( action = '+' , amount = 5 ))
menu . add_button ( skip )
# ViewButton.ID_PREVIOUS_PAGE
back_button = ViewButton ( style = discord . ButtonStyle . primary , label = 'Back' , custom_id = ViewButton . ID_PREVIOUS_PAGE )
menu . add_button ( back_button )
# ViewButton.ID_NEXT_PAGE
next_button = ViewButton ( style = discord . ButtonStyle . secondary , label = 'Next' , custom_id = ViewButton . ID_NEXT_PAGE )
menu . add_button ( next_button )
# All other ViewButton are created the same way as the last 2 EXCEPT
# 1 - ViewButton.ID_CALLER
# 2 - ViewButton.ID_SEND_MESSAGE
# 3 - ViewButton.ID_CUSTOM_EMBED
# ViewButton.ID_CALLER
def say_hello ( name : str ):
print ( 'Hello' , name )
call_followup = ViewButton . Followup ( details = ViewButton . Followup . set_caller_details ( say_hello , 'John' ))
menu . add_button ( ViewButton ( label = 'Say hi' , custom_id = ViewButton . ID_CALLER , followup = call_followup ))
# ViewButton.ID_SEND_MESSAGE
msg_followup = ViewButton . Followup ( 'This message is hidden!' , ephemeral = True )
menu . add_button ( ViewButton ( style = discord . ButtonStyle . green , label = 'Message' , custom_id = ViewButton . ID_SEND_MESSAGE , followup = msg_followup ))
# ViewButton.ID_CUSTOM_EMBED
custom_embed_button = ViewButton ( style = discord . ButtonStyle . blurple , label = 'Social Media Info' , custom_id = ViewButton . ID_CUSTOM_EMBED , followup = ViewButton . Followup ( embed = discord . Embed (...)))หมายเหตุ: เมื่อมันมาถึงปุ่มด้วย
custom_idของViewButton.ID_CALLER,ViewButton.ID_SEND_MESSAGE,ViewButton.ID_CUSTOM_EMBEDหรือปุ่มลิงก์คุณสามารถเพิ่มได้มากเท่าที่คุณต้องการตราบเท่าที่ทั้งหมด 25 ปุ่มหรือน้อยกว่า สำหรับรหัสปุ่มอื่น ๆ ทั้งหมดแต่ละเมนูสามารถมีได้เท่านั้น
เลือกใช้เมื่อคุณต้องการจัดหมวดหมู่ข้อมูลในเมนูของคุณ เลือกสามารถใช้งานได้เฉพาะเมื่อเมนูเมนู menu_type ถูก TypeEmbed คุณควรจำไว้ว่าข้อ จำกัด ของความไม่ลงรอยกันเกี่ยวกับจำนวนเมนู UI รายการ (แถว) สามารถนำไปใช้กับแต่ละข้อความได้
Page.from_embeds(embeds: Sequence[Embed])ViewMenu.add_select(select: ViewSelect)ViewMenu.remove_select(select: ViewSelect)ViewMenu.remove_all_selects()ViewMenu.disable_select(select: ViewSelect)ViewMenu.disable_all_selects()ViewMenu.enable_select(select: ViewSelect)ViewMenu.enable_all_selects()ViewMenu.get_select(title: Union[str, None])ตัวอย่าง:
from reactionmenu import ViewMenu , ViewSelect , Page
menu = ViewMenu ( ctx , menu_type = ViewMenu . TypeEmbed )
menu . add_page ( discord . Embed ( title = "A showcase of console video games" , color = discord . Color . blurple ()))
menu . add_select ( ViewSelect ( title = "Console Video Games" , options = {
# NOTE: The discord.SelectOption parameter "default" cannot be set to True
discord . SelectOption ( label = "PlayStation" , emoji = "<:PlayStation:549638412538478602>" ) : [
Page ( embed = discord . Embed ( title = "Ratchet & Clank" , description = ..., color = discord . Color . yellow ()). set_image ( url = ...)),
Page ( embed = discord . Embed ( title = "God of War" , description = ..., color = discord . Color . blue ()). set_image ( url = ...))
],
discord . SelectOption ( label = "Xbox" , emoji = "<:Xbox:501880493285834752>" ) : [
Page ( embed = discord . Embed ( title = "Halo Infinite" , description = ..., color = discord . Color . green ()). set_image ( url = ...)),
Page ( embed = discord . Embed ( title = "Gears of War 4" , description = ..., color = discord . Color . red ()). set_image ( url = ...))
]
}))
menu . add_button ( ViewButton . back ())
menu . add_button ( ViewButton . next ())
await menu . start ()คุณสามารถใช้การเลือกประเภทนี้เมื่อคุณต้องการใช้ UI เพื่อเลือกหน้าเพื่อไปที่
ViewMenu.add_go_to_select(goto: ViewSelect.GoTo)ViewMenu.enable_go_to_select(goto: ViewSelect.GoTo)ViewMenu.enable_all_go_to_selects()ViewMenu.disable_go_to_select(goto: ViewSelect.GoTo)ViewMenu.disable_all_go_to_selects()ViewMenu.remove_go_to_select(goto: ViewSelect.GoTo)ViewMenu.remove_all_go_to_selects() พารามิเตอร์ page_numbers สำหรับ ViewSelect.GoTo สามารถใช้กับ 3 ประเภทที่แตกต่างกัน
List[int] หากตั้งค่าเป็นรายการจำนวนเต็มค่าที่ระบุเหล่านั้นเป็นตัวเลือกเดียวที่พร้อมใช้งานเมื่อถูกคลิกpage_numbers=[1, 5, 10]Dict[int, Union[str, discord.Emoji, discord.PartialEmoji]] คุณสามารถใช้ประเภทนี้ได้หากคุณต้องการใช้อิโมจิในการเลือกของคุณpage_numbers={1 : "?️", 2 : ""}ellipsis คุณสามารถตั้งค่าจุดไข่ปลา ที่แท้จริง เพื่อให้ห้องสมุดกำหนดหมายเลขหน้าทั้งหมดทั้งหมดให้เป็นจำนวนหน้าเว็บที่คุณเพิ่มลงในเมนู สิ่งนี้มีประโยชน์ถ้าคุณมี 25 หน้าหรือน้อยกว่าpage_numbers=...หมายเหตุ : การตั้งค่าพารามิเตอร์
page_numbersเป็นจุดไข่ปลา (... ) ใช้งานได้ตามที่ตั้งใจไว้หากคุณได้เพิ่มการเลือกเพื่อเลือกหลังจากคุณเพิ่มหน้าลงในเมนู
@ bot . command ()
async def navigate ( ctx ):
menu = ViewMenu ( ctx , menu_type = ViewMenu . TypeEmbed )
menu . add_page ( discord . Embed ( title = "Twitter" ). set_image ( url = "..." ))
menu . add_page ( discord . Embed ( title = "YouTube" ). set_image ( url = "..." ))
menu . add_page ( discord . Embed ( title = "Discord" ). set_image ( url = "..." ))
# ...
menu . add_go_to_select ( ViewSelect . GoTo ( title = "Go to page..." , page_numbers = ...))
menu . add_button ( ViewButton . back ())
menu . add_button ( ViewButton . next ())
await menu . start ()await ViewMenu.refresh_menu_items()await ViewMenu.update(*, new_pages: Union[List[Union[Embed, str]], None], new_buttons: Union[List[ViewButton], None]) เมื่อเมนูทำงานอยู่คุณสามารถอัปเดตหน้าหรือปุ่มบนเมนู การใช้ ViewMenu.update() คุณสามารถแทนที่หน้าและปุ่ม การใช้ ViewMenu.refresh_menu_items() อัปเดตปุ่มที่คุณเปลี่ยนไป
@ bot . command ()
async def menu ( ctx ):
menu = ViewMenu (..., name = 'test' )
link_button = ViewButton (..., label = 'Link' )
menu . add_button ( link_button )
menu . add_page (...)
await menu . start ()
@ bot . command ()
async def disable ( ctx ):
menu = ViewMenu . get_session ( 'test' )
link_button = menu [ 0 ]. get_button ( 'Link' , search_by = 'label' )
menu . disable_button ( link_button )
await menu . refresh_menu_items () หากปุ่มไม่ได้รีเฟรชด้วย ViewMenu.refresh_menu_items() เมนูจะไม่ได้รับการอัปเดตเมื่อเปลี่ยนปุ่ม
วิธีการ ViewMenu.update(...) ใช้เมื่อคุณต้องการแทนที่ปุ่มทั้งหมดหรือสองสามปุ่มในเมนู
menu = ViewMenu (...)
# in a different .command()
await menu . update ( new_pages = [ hello_embed , goodbye_embed ], new_buttons = [ link_button , next_button ])หมายเหตุ : เมื่อใช้
ViewMenu.update(...)ไม่จำเป็นต้องใช้ViewMenu.refresh_menu_items()เนื่องจากได้รับการอัปเดตระหว่างการอัพเดต
คลาส ViewButton มาพร้อมกับวิธีการตั้งค่าโรงงาน (วิธีการเรียน) ที่ส่งคืน ViewButton พร้อมพารามิเตอร์ที่ตั้งค่าตาม custom_id (ไม่รวมปุ่มลิงก์)
ViewButton.link(label: str, url: str)style : discord.ButtonStyle.linklabel : <label>url : <url>ViewButton.back()style : discord.ButtonStyle.graylabel : "ย้อนกลับ"custom_id : ViewButton.ID_PREVIOUS_PAGEViewButton.next()style : discord.ButtonStyle.graylabel : "ถัดไป"custom_id : ViewButton.ID_NEXT_PAGEViewButton.go_to_first_page()style : discord.ButtonStyle.graylabel : "หน้าแรก"custom_id : ViewButton.ID_GO_TO_FIRST_PAGEViewButton.go_to_last_page()style : discord.ButtonStyle.graylabel : "หน้าสุดท้าย"custom_id : ViewButton.ID_GO_TO_LAST_PAGEViewButton.go_to_page()style : discord.ButtonStyle.graylabel : "การเลือกหน้า"custom_id : ViewButton.ID_GO_TO_PAGEViewButton.end_session()discord.ButtonStyle.grayViewButton.ID_END_SESSIONViewButton.all()list ViewButton ตามลำดับต่อไปนี้.go_to_first_page() .back() .next() .go_to_last_page() .go_to_page() .end_session()ViewButton.all_with_emojis()list ViewButton ด้วยพารามิเตอร์ emoji ของพวกเขาที่ตั้งไว้แล้วในลำดับต่อไปนี้.go_to_first_page() .back() .next() .go_to_last_page() .go_to_page() .end_session()ViewButton.generate_skip(label: str, action: str, amount: int)style : discord.ButtonStyle.graylabel : <label>custom_id : ViewButton.ID_SKIPskip : ViewButton.Skip(<action>, <amount>) menu = ViewMenu ( ctx , ...)
menu . add_page (...)
menu . add_page (...)
menu . add_button ( ViewButton . back ())
menu . add_button ( ViewButton . next ())
await menu . start () คุณสามารถตั้งค่า ViewButton ที่จะปิดใช้งานหรือลบออกเมื่อมีการกดจำนวนครั้งที่กำหนด
class ViewButton.Event(event_type: str, value: int)
event_type ( str ) การกระทำที่จะดำเนินการ สามารถเป็น "ปิดใช้งาน" หรือ "ลบ" ได้value ( int ) จำนวนเงินที่ตั้งไว้สำหรับเหตุการณ์ที่ระบุ ต้องเป็น> = 1. ถ้าค่าคือ <= 0 มันจะถูกตั้งค่าเป็น 1 โดยปริยายเป็น 1ตัวอย่าง:
menu = ViewMenu ( ctx , ...)
# disable a button after 5 clicks
button_1 = ViewButton (..., event = ViewButton . Event ( 'disable' , 5 ))
menu . add_button ( button_1 )
# remove a button after 10 clicks
button_2 = ViewButton (..., event = ViewButton . Event ( 'remove' , 10 ))
menu . add_button ( button_2 )หมายเหตุ: ไม่ถูกต้องสำหรับปุ่มลิงก์ ยังไม่เหมาะสำหรับปุ่มที่มี
custom_idของViewButton.ID_END_SESSION
รีเลย์เมนูเป็นฟังก์ชั่นที่เรียกว่าทุกเวลาปุ่มที่แยกออกจากเมนูจะถูกกด ถือว่าเป็นส่วนขยายของ ViewButton ที่มี ID ของ ViewButton.ID_CALLER ซึ่งแตกต่างจากปุ่มผู้โทรซึ่งไม่มีรายละเอียดเกี่ยวกับการโต้ตอบในเมนูรีเลย์ทำ
ViewMenu.set_relay(func: Callable[[NamedTuple], None], *, only: Optional[List[ViewButton]]=None)ViewMenu.remove_relay() เมื่อสร้างฟังก์ชั่นสำหรับรีเลย์ของคุณฟังก์ชั่นนั้นจะต้องมีอาร์กิวเมนต์ตำแหน่งเดียว เมื่อกดปุ่มวัตถุ RelayPayload (tuple ที่มีชื่อ) จะถูกส่งผ่านไปยังฟังก์ชั่นนั้น แอตทริบิวต์ของ RelayPayload คือ:
member ( discord.Member ) บุคคลที่กดปุ่มbutton ( ViewButton ) ปุ่มที่ถูกกดตัวอย่าง:
async def enter_giveaway ( payload ):
member = payload . member
channel = payload . button . menu . message . channel
await channel . send ( f" { member . mention } , you've entered the giveaway!" )
menu = ViewMenu ( ctx , ...)
menu . set_relay ( enter_giveaway ) วิธี set_relay มาพร้อมกับพารามิเตอร์ only หากพารามิเตอร์นั้น None ปุ่มทั้งหมดที่กดจะถูกส่ง (ยกเว้นปุ่มลิงก์เพราะพวกเขาไม่ส่งเหตุการณ์การโต้ตอบ) คุณสามารถระบุ list ปุ่มไปยังพารามิเตอร์นั้นได้ดังนั้นจะมีเพียงปุ่มกดจากปุ่มที่ระบุเหล่านั้นเท่านั้นที่จะถูกถ่ายทอด
def example ( payload ):
...
menu = ViewMenu ( ctx , ...)
back_button = ViewButton . back ()
next_button = ViewButton . next ()
menu . set_relay ( example , only = [ back_button ])await ViewMenu.start(*, send_to=None, reply=False)await ViewMenu.stop(*, delete_menu_message=False, remove_buttons=False, disable_buttons=False) เมื่อเริ่มต้นเมนูคุณมีตัวเลือกในการส่งเมนูไปยังช่องทางหนึ่ง พารามิเตอร์ send_to เป็นช่องที่คุณต้องการส่งเมนูไป คุณสามารถตั้ง send_to เป็นชื่อช่อง ( str ), ID ช่อง ( int ) หรือวัตถุช่อง ( discord.TextChannel / discord.Thread ) ตัวอย่าง:
menu = ViewMenu (...)
# channel name
await menu . start ( send_to = 'bot-commands' )
# channel ID
await menu . start ( send_to = 1234567890123456 )
# channel object
channel = guild . get_channel ( 1234567890123456 )
await menu . start ( send_to = channel )
# there's no need to specify send_to unless you want the menu to be sent to a different channel
# from the one you're sending the initial message/using the command in. the menu can be started
# in the current channel by omitting the send_to parameter
await menu . start ()หมายเหตุ:
send_toไม่ถูกต้องหากเมนูเริ่มต้นใน DM's
มีตัวเลือกเดียวเท่านั้นเมื่อหยุดเมนู หากคุณมีพารามิเตอร์หลายตัวเป็น True มีเพียงหนึ่งเดียวเท่านั้นที่จะดำเนินการ
delete_menu_message > disable_buttonsdisable_buttons > remove_buttons นี่คือการใช้งานพื้นฐานของ ViewMenu ที่คุณสามารถคัดลอกและวางเพื่อการสาธิตอย่างรวดเร็ว
import asyncio
import discord
from discord . ext import commands
from reactionmenu import ViewMenu , ViewButton
bot = commands . Bot ( command_prefix = '!' , intents = discord . Intents . all ())
async def start_bot ():
async with bot :
await bot . start ( '...' )
@ bot . command ()
async def example ( ctx ):
menu = ViewMenu ( ctx , menu_type = ViewMenu . TypeEmbed )
for member in ctx . guild . members :
if member . avatar :
embed = discord . Embed ( description = f'Joined { member . joined_at . strftime ( "%b. %d, %Y" ) } ' )
embed . set_author ( name = member . name , icon_url = member . avatar . url )
menu . add_page ( embed )
menu . add_button ( ViewButton . back ())
menu . add_button ( ViewButton . next ())
menu . add_button ( ViewButton . end_session ())
await menu . start ()
asyncio . run ( start_bot ())