'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!
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sxviaat |
