how can I display such a button through the code in not through #BotFather
I use aiogram
Now I have default commands like
/start & /help
async def set_default_commands(dp):
await dp.bot.set_my_commands(
[
types.BotCommand("start", "Старт бота"),
types.BotCommand("help", "Помощь"),
]
)
Related
#dp.message_handler(lambda message: message.text == "Python")
async def python(message: types.Message):
keyboard: InlineKeyboardMarkup = InlineKeyboardMarkup(resize_keyboard=True)
url_button_1: InlineKeyboardButton = InlineKeyboardButton(text=f"Road", url='https://***')
url_button_2: InlineKeyboardButton = InlineKeyboardButton(text=f"Tasks", url='https://***')
url_button_3: InlineKeyboardButton = InlineKeyboardButton(text=f"Books", url='https://***')
url_button_4: InlineKeyboardButton = InlineKeyboardButton(text=f"Text", url='https://***')
keyboard.add(url_button_1)
keyboard.add(url_button_2)
keyboard.add(url_button_3)
keyboard.add(url_button_4)
await message.answer(text=f"Select the necessary sources", reply_markup=keyboard)
How do I make sure that by clicking on any of these buttons, a picture is immediately displayed to the user in the chat?
With this option, you still need to click on the link, which works longer:
url_button_2: InlineKeyboardButton = InlineKeyboardButton(text=f"Tasks", url='https://***')
I recently studied the buttons, and I can't figure out how to get the user who clicked on the interaction button in discord.py
Here is my code:
class NewView(View):
def __init__(self):
super().__init__(timeout=None)
#button(label='First Button', custom_id='button_one', style=discord.ButtonStyle.red)
async def buttonone(self, button: Button, interaction: discord.Interaction):
await interaction.response.send_message(f"Hello, {button.author.display_name}")
#bot.command()
async def callbutton(ctx: commands.Context):
await ctx.send('Your Button:', view=NewView())
You may use the .user attribute to extract the user who sent that interaction:
Interactions API Reference
Discord Developer Portal - Documentation - Interaction
In your button decorator you can do print(interaction.user) and it will print user and tag of user that pushed it. if you want there id you can do print(interaction.user.id)
I am not able to receive a response message when send a message in Discord? I think I have something wrong at here interaction.send("Hello!)
import nextcord
from nextcord import Button, Interaction
from nextcord.ext import commands
from config import DISCORD_ALERT_TOKEN
TESTING_GUILD_ID = 123456789 # Replace with your own guild IDs
client = commands.Bot(command_prefix="?")
#client.event
async def on_ready():
print(f"{client.user.name} has connected to Discord.")
await client.wait_until_ready()
channel = client.get_channel(int(966316317890207779))
await channel.send(f"{client.user.name} has connected to Discord.")
#client.slash_command(description="My first slash command", guild_ids=[TESTING_GUILD_ID])
async def hello(interaction: nextcord.Integration):
await interaction.send("Hello!")
client.run(DISCORD_ALERT_TOKEN)
So after searching I found the problem.
If the scope application command is unchecked it will not work.
You then have to select this and invite the bot again.
I'm creating a simple Twitch bot for personal use. I'm using twitchio.ext commands. Everything works fine, I'm connected, I'm able to print all the messages from the chat, my bot responds to commands but I'm not able to send a message to chat and I don't know why.
from twitchio.ext import commands
class Bot(commands.Bot):
def __init__(self):
super().__init__(token='oauth:censored', prefix='g ', nick = "nick of the bot", irc_token = "censored", initial_channels=['channel'])
async def event_ready(self):
print(f'Using bot as {self.nick}')
#commands.command()
async def test(self, ctx: commands.Context):
await ctx.send('test')
print("printed")
bot = Bot()
#bot.event()
async def event_message(msg):
print(msg.author.name)
print(msg.content)
await bot.handle_commands(msg)
bot.run()
When I type "g test" in chat, the message "test" is not sent but the message "printed" is printed to the console. Do you know where could be the problem? Also, I would like to ask if there is any way how to send a message to chat directly without responding to a command or event.
Try adding the bot as a mod
The 'nick' in the bot init() function doesn't seem to do anything the nick is linked to your IRC username.
I'm assuming the channel in your initial_channels in the init() func isn't actually the channel you put there since you said you were able to type in the chat.
If the channel that you are using the bot from(linked to the oauth key) is not the same channel as you are connecting to in the chat then you probably need to add the bots username as a moderator in that chat
try
/mod [bot_username]
in the chat as the channel owner to do this
Also if your bot is the same account you are sending commands from on twitch the rate limit will be reached unless they are a mod. So either make a dedicated account for your bot or mod the bot on the server
I'm trying to create a verification function for Discord Bot using buttons. I have tried creating it, but not sure if it works (testing purpose) .
I need it to give a role to verify a user like a role called Verified, would also like a suggestion on how I can make it not a command and just make it a embed in a single channel where new members can just simply click on it and get the role to be verified.
I'm not getting any errors in the console of development, it's just saying when I click the button in the UI (Discord App) I get an message saying "Interaction failed".
#client.command()
async def verify(ctx):
member = ctx.message.author
role = get(member.guild.roles, name="Sexy EGirl")
await ctx.send(
embed = discord.Embed(description="Click below to verify", color=getcolor(client.user.avatar_url)),
components = [
Button(style=ButtonStyle.blue, label = 'Verify')
]
)
interaction = await client.wait_for("button_click", check=lambda i: i.component.label.startswith("Verify"))
await interaction.respond(member.add_roles(role))
You get "Interaction failed", if you don't send a respond message.
#client.event
async def on_button_click(interaction):
message = interaction.message
button_id = interaction.component.id
if button_id == "verify_button":
member = message.guild.get_member(interaction.user.id)
role = message.guild.get_role(859852728260231198) # replace role_id with your roleID
await member.add_roles(role)
response = await interaction.respond(embed=discord.Embed(title="Verified"))
#client.command()
#commands.has_permissions(administrator=True) # you need to import command from discord.ext if you want to use this -> from discord.ext import commands
async def verify(ctx):
await ctx.send(
embed=discord.Embed(title="Embed - Title", description="Click below to verify"),
components=[
Button(style=ButtonStyle.blue, label='Verify', custom_id="verify_button")
])
With this code you are able to send an embed with a verify button in a channel. (if you have administrator permissions) Then the user can press on the button and the on_button_click event will be triggered.
To handle the Interaction failed, you are sending a respond message.