[英]Nextcord send a message to a specific channel (discord.py fork)
所以,我一直在嘗試提出建議 discord 機器人,因此它會在提示時向特定頻道發送消息並通過按鈕進行驗證,但我似乎無法使其工作。 我的代碼在這里:
import nextcord
import os
import requests
import asyncio
import random
import catapi
import json
import nest_asyncio
nest_asyncio.apply()
apicat = catapi.CatApi(api_key=os.environ['CAT_API_KEY'])
loop = asyncio.new_event_loop()
intents = nextcord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '$', intents=intents)
colors = [0x00ffee,0x32a852,0x4287f5,0xcc2af5,0xf7e7a6,0xdea4a2]
def run_coro(coroutine):
return loop.run_until_complete(coroutine)
class YesOrNo(nextcord.ui.View):
def __init__(self):
super().__init__()
self.value = None
@nextcord.ui.button(emoji="✔", style=nextcord.ButtonStyle.success)
async def yes(self, button: nextcord.ui.Button, interaction: Interaction):
channel = client.get_channel(950111018405748746)
embed = nextcord.Embed(title="Suggestion was sent!", color=0x51ff00)
await channel.send("test")
await interaction.edit(embed=embed, view=None)
self.value = True
self.stop()
@nextcord.ui.button(emoji="✖️", style=nextcord.ButtonStyle.danger)
async def no(self, button: nextcord.ui.Button, interaction: Interaction):
embed = nextcord.Embed(title="Suggestion was discarded!", color=0xff0000)
await interaction.edit(embed=embed, view=None)
self.value = False
self.stop()
但我收到此錯誤: AttributeError: 'YesOrNo' object 沒有屬性 'client' 有什么解決辦法嗎? 我試過將所有客戶端和東西更改為機器人,我試過將客戶端放入 init、super init、class、ui.button、async def,但我仍然不知道出了什么問題!
傳遞client
然后用self.client
定義它,像這樣:
class YesOrNo(nextcord.ui.View):
#pass `client` here
↓
def __init__(self):
super().__init__()
self.value = None
self.client = client # ← then define self.client
您的代碼將如下所示:
class YesOrNo(nextcord.ui.View):
def __init__(client, self):
super().__init__()
self.value = None
self.client = client
@nextcord.ui.button(emoji="✔", style=nextcord.ButtonStyle.success)
async def yes(self, button: nextcord.ui.Button, interaction: Interaction):
channel = client.get_channel(950111018405748746)
embed = nextcord.Embed(title="Suggestion was sent!", color=0x51ff00)
await channel.send("test")
await interaction.edit(embed=embed, view=None)
self.value = True
self.stop()
@nextcord.ui.button(emoji="✖️", style=nextcord.ButtonStyle.danger)
async def no(self, button: nextcord.ui.Button, interaction: Interaction):
embed = nextcord.Embed(title="Suggestion was discarded!", color=0xff0000)
await interaction.edit(embed=embed, view=None)
self.value = False
self.stop()
大概就是這樣。
• Sxviaat
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.