Nextcord 向特定频道发送消息 (discord.py fork)
Nextcord send a message to a specific channel (discord.py fork)
所以,我一直在尝试提出建议 discord bot,因此它会在提示时向特定频道发送消息并使用按钮进行验证,但我似乎无法使其工作。
我的代码在这里:
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' 对象没有属性 '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()
This is probably it.
• Sxviaat
所以,我一直在尝试提出建议 discord bot,因此它会在提示时向特定频道发送消息并使用按钮进行验证,但我似乎无法使其工作。 我的代码在这里:
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' 对象没有属性 '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()
This is probably it.
• Sxviaat