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)
Related
import discord
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
content = message.content.lower().strip():
if "egg" not in content:
await message.delete()
I tried:
async def on_message(message):
if "egg" in message.content.lower():
return
await message.delete()`
what am I doing wrong?
first time making a discord bot. I did search a LOT, most questions are only about how TO delete messages containing specific words, not the other way around.
appreciate any help, thanks.
I don't know why I'm getting this error. I have all the intents enabled as seen in the image below.
This is my first project** in discord.py, and I don't really understand some things.
Here is the code I have (hoping this is relavant):
import nextcord
from nextcord import Interaction
from nextcord.ext import commands
intents = nextcord.Intents.default()
intents.members = True
command_prefix = '!'
client = commands.Bot(command_prefix, intents = intents)
#client.event
async def on_ready():
print('logged in as {0.user}'.format(client))
print(command_prefix)
testserver = 988598060538015815
#nextcord.slash_command(name = "hello", description = "Bot Says Hello", guild_ids = [testserver])
async def hellocommand(interaction: Interaction):
await interaction.response.send_message("yoooo")
client.run("my token")
change intents = nextcord.Intents.default() to intents = nextcord.Intents.all()
I have a function that checks if 'your' is changed to 'you're' and I am having trouble having the bot send a message after this check. What is the proper way to do this? (I want this to apply to all channels.)
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
#client.event
async def on_message_edit(before,after):
if(before.content == "your" and after.content == "you're"):
# I want to send message here
client.run('Token') # I have my token here
Add ctx into the parameters (before,after,ctx), and
then put in await ctx.send("message") where you want your message to send.
the parameters before and after are of type discord.Message. discord.Message has an attribute channel, with which you can use the function send.
#client.event
async def on_message_edit(before, after):
if before.content == 'your' and after.content == "you're":
await before.channel.send(send_whatever_you_want)
I'm working on a bot and the on_member_join function isn't working when a user joins the server. My function is in a cog and is the only function that won't work,
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
#commands.Cog.listener()
async def on_member_join(self, member):
print('working')
I am assuming the indentation is just wrong while pasting here and not in the actual code.
If you haven't done this already, the new version of discord.py has something called intents. First you have to go to the developer portal and activate it. Then you have to add the intents to your code.
This will enable all the Intents:
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '-', intents=intents)
I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)