#client.event
async def on_message(message):
if client.user.mentioned_in(message):
embed = discord.Embed(title='Im here boss', description='Prefix: `n!help`', color=discord.Colour.from_rgb(249, 35, 142))
embed.set_image(url='https://i.pinimg.com/originals/fe/1d/60/fe1d606ca0bcaf954a78cae3ed9c628b.gif')
embed.set_footer(icon_url = ctx.author.avatar_url, text = f'Brought here by {ctx.author.name}')
await message.channel.send(embed=embed)
If i add ctx in the async def thing it shows an error and that it isnt defined and also in the footer it isnt defined. how do i define ctx in this thing here?
You can use the get_context method.
Wherever you need to get the ctx variable, add something like this:
ctx = await client.get_context(message)
You can also input a custom context as a second argument (cls=MyCustomCtx)
Instead of using ctx.author.* use message.author.*, though if you really want to get the Context (ctx) use the following:
ctx = await client.get_context(message)
Related
async def afk(self, ctx, *args):
msg = ' '.join(args)
self.data.append(ctx.author.id)
self.data.append(msg)
await ctx.author.edit(nick=f'[AFK] {ctx.author.name}')
await ctx.send("afk set!")
#commands.Cog.listener()
async def on_message(self, message):
for i in range(len(self.data)):
if (f"<#{self.data[i]}>" in message.content) and (not message.author.bot):
await message.channel.send(f"<#{self.data[i]}> is away right now, they said: {self.data[i+1]}")
return None
break
#commands.Cog.listener()
async def on_typing(self, channel, user, when):
if user.id in self.data:
i = self.data.index(user.id)
self.data.remove(self.data[i+1])
self.data.remove(user.id)
nick = ctx.author.name.replace('[AFK]', '')
await ctx.author.edit(nick=nick)
await channel.send(f"{user.mention}, Welcome back!")
But it is showing ctx is not found nick = ctx.author.name.replace('[AFK]', '') i have been trying various methods to solve it, but i am not able to fix this, pls help me
You're using ctx in a func which doesnt have a ctx arg, you should instead use
nick = user.author.name.replace('[AFK]', '')
await user.author.edit(nick=nick)
I'm facing problems trying to add a reaction to the bot's response when it responds to a slash command I entered.
#bot.slash_command(name='test', description="Test.")
async def test(ctx):
msg = await ctx.send("Hello.")
await msg.add_reaction('🤖')
As you can see it's supposed to add a reaction to it's own message.
But I get this error:
nextcord.errors.ApplicationInvokeError: Command raised an exception: AttributeError: 'PartialInteractionMessage' object has no attribute 'add_reaction'
Please tell me how do I add a reaction to a slash command.
Explanation
As per the error, ctx.send is returning a PartialInteractionMessage. From the docs:
This does not support most attributes and methods of nextcord.Message. The fetch() method can be used to retrieve the full InteractionMessage object.
Code
#bot.slash_command(name='test', description="Test.")
async def test(ctx):
msg = await ctx.send("Hello.")
full_msg = await msg.fetch()
await full_msg.add_reaction('🤖')
I am creating a bot that would create a number of buttons based on a for loop, this is what I want to succeed in doing:
(I'm using discord_components, for some reason, there isn't a tag for that, idk why)
import discord
from discord_components import *
client = discord.Client()
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
client = commands.Bot(command_prefix = "!", intents = intents)
client.remove_command('help')
#client.event
async def on_ready():
print("ok")
DiscordComponents(client)
#client.command()
async def create_buttons(ctx, numofbuttons: int, label):
buttons = []
for i in range(numofbuttons):
buttons.append(Button(style=ButtonStyle.blue,label=label))
await ctx.send("this message has buttons!",components=buttons)
a = await client.wait_for("button_click")
await ctx.send(f"{a.label} was clicked!")
this didn't work, I assumed because when you add a function to a list, you are adding what it returns instead of the function itself(if you know what I mean, sorry I'm not an expert at this stuff). is there any way to fix this? the problem I'm dealing with is more complex and absolutely has to include a for loop. I created the sample above for a simpler version that is much more easier to read. I am using "await client.wait_for" because for the real project, I need a way to get the button's label
First, name the function properly, put an underscore between create and button
Marking the numberofbuttons parameter as int removes raising of TypeError.
Putting the * before label marks the label parameter as a multi-line parameter.
You can use the view module to add buttons, this is an example:
import discord
from discord.ui import Button, View
client = discord.Client()
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
client = commands.Bot(command_prefix = "!", intents = intents)
client.remove_command('help')
#client.command()
async def create_buttons(ctx, numofbuttons: int, *, label: str):
buttons = []
view = View()
for i in range(numofbuttons):
view.add_item(Button(style=ButtonStyle.blue, label=label))
await ctx.send("this message has buttons!",view=view)
How do you send a normal message when a user types the command? I have tried using #client.command
async def ping
but it does not work D:
You can use await ctx.send("Your message here"), make sure that you have Context passed in your #client.command.
If you have anymore questions, feel free to ask me :)
Use this as a guide if it helps
#client.command
async def ping(ctx):
await ctx.send("Your message")
here ctx is the context and hi is command
Code:
#client.command
async def hi(ctx):
await ctx.send("Message which you want to send")
So I am using MurkAPI and had gotten help with this but I cannot seem to get it so when someone does $adfly and a url which is a adfly shortend link, the bot returns the link through the API and into the bot. This is the current code I have.
#commands.command(pass_context=True)
async def adfly(self, ctx):
async with aiohttp.ClientSession() as session:
await self.client.say(await fetch_adfly(session))
async def fetch_adfly(session):
async with session.get(adfly_url) as response:
return await response.text()
I have the MURKKEY working, however, I cannot get the URL part working.
adfly_url = 'https://murkapi.com/adfly.php?key={}&url={}'.format(MURKKEY)
If fetch_adfly is meant to handle transforming your url into an adfly url, you want the session in that function. Instead of passing a session, we want to pass the url that we want transformed. That might give us something like:
async def fetch_adfly(url):
async with aiohttp.ClientSession() as session:
adfly_url = 'https://murkapi.com/adfly.php?key={}&url={}'.format(MURKKEY, url)
async with session.get(adfly_url) as response:
return await response.text()
Then, you want to add a url parameter to your command and pass it to your function:
#commands.command(pass_context=True)
async def adfly(self, ctx, url):
await self.client.say(await fetch_adfly(url))
FWIW, I would suggest validating URLs.