简体   繁体   English

Nextcord 向特定频道发送消息(discord.py fork)

[英]Nextcord send a message to a specific channel (discord.py fork)

So, I've been trying to make a suggestion discord bot, so it sends a message to a specific channel when prompted and verified with a button, but I can't seem to make it work.所以,我一直在尝试提出建议 discord 机器人,因此它会在提示时向特定频道发送消息并通过按钮进行验证,但我似乎无法使其工作。 My code is here:我的代码在这里:

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()

But I receive this error: AttributeError: 'YesOrNo' object has no attribute 'client' Any ideas how to fix it?但我收到此错误: AttributeError: 'YesOrNo' object 没有属性 'client' 有什么解决办法吗? I've tried changing all the clients and stuff to bot, I've tried putting client inside the init, super init, class, ui.button, async def, and I STILL DO NOT KNOW WHAT'S WRONG!我试过将所有客户端和东西更改为机器人,我试过将客户端放入 init、super init、class、ui.button、async def,但我仍然不知道出了什么问题!

Pass client and then define it with self.client , like this:传递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

Your code will look like this:您的代码将如下所示:

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 • Sxviaat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM