简体   繁体   中英

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. 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? 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!

Pass client and then define it with self.client , like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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