discord.py I want to know emoji url but It couldn't work - discord.py

import discord
from discord.ext import commands
from discord import app_commands
class emoji(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
#app_commands.command(name = "emoji", description = "test")
async def emoji(self, interaction: discord.Interaction, emoji: discord.Emoji):
await interaction.response.send_message(emoji.url)
async def setup(bot: commands.Bot):
await bot.add_cog(emoji(bot))
I don't know why it can't work..
I think its right way

Related

discord.py will not execute a command for some reason

whenever I try to type something in discord, the bot will do nothing.
I checked on the discord developer portal if the message intents was on and it was and I also enabled it in the code as you can see above but it still doesn't work
import os
import discord
import random
from keep_alive import keep_alive
from discord.ext import commands
token = 'token'
intents = discord.Intents.all()
intents.members = True
command_prefix = '!'
bot = commands.Bot(command_prefix, intents = intents)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(
type=discord.ActivityType.watching, name='butterflys fly by'))
#bot.command(name='99')
async def nine_nine(ctx):
print("hell world")
brooklyn_99_quotes = [
'I\'m the human form of the 💯 emoji.',
'Bingpot!',
(
'Cool. Cool cool cool cool cool cool cool, '
'no doubt no doubt no doubt no doubt.'
),
]
response = random.choice(brooklyn_99_quotes)
await ctx.send(response)
#bot.event
async def on_member_join(member):
guild = bot.get_guild(781230819414769765)
welcome_channel = guild.get_channel(783371093570355211)
await welcome_channel.send(
f'Welcome to **{guild.name}**, {member.mention} have fun here! ')
keywords = ["e"]
#bot.event
async def on_message(msg):
for i in range(len(keywords)):
if keywords[i] in msg.content:
await msg.channel.purge(limit=1)
await msg.author.send("don't say that!")
keep_alive()

Discord.py ghost TEST command

The bot has a "ghost" test command. I do not create it anywhere, yet still it shows up in the slash command menu. If I try to run it I get an error, that the command doesn't exist.
import asyncio
import discord
from discord.ext import commands
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.all()
super().__init__(command_prefix="$", intents=intents)
async def setup_hook(self):
await self.tree.sync()
print(f"Synced slash commands for {self.user}.")
async def on_command_error(self, ctx, error):
print(error)
await ctx.reply(error, ephemeral=True)
bot = Bot()
async def main():
async with bot:
await bot.start('Token')
asyncio.run(main())
If I try to create it I get two /test commands
import asyncio
import discord
from discord.ext import commands
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.all()
super().__init__(command_prefix="$", intents=intents)
async def setup_hook(self):
await self.tree.sync()
print(f"Synced slash commands for {self.user}.")
async def on_command_error(self, ctx, error):
print(error)
await ctx.reply(error, ephemeral=True)
bot = Bot()
#bot.hybrid_command(name="test", with_app_command=True, description="Testing")
async def test(ctx: commands.Context):
await ctx.defer(ephemeral=True)
await ctx.reply("hi!")
async def main():
await bot.start('Token')
asyncio.run(main())
Where does the first test command come from?
Showing in the menu\Two /test commands

discord.py add_roles function not working

Attempting to create a jail bot that sends user to the "APOS zone" when given the role "APOS", but the bot is unresponsive when the command is sent
import os
import discord
import random
from discord.ext import commands
import keep_alive
from discord.utils import get
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
Bot = commands.Bot(command_prefix='AGOP~')
#startup
#bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("AGOP_Bot is in " + str(guild_count) + " guilds.")
#APOS
#Bot.command()
#commands.has_role("APOS")
async def APOS(ctx, user: discord.Member, role: discord.Role):
role = get(user.server.roles, name="APOS")
await user.add_roles(user, role)
await ctx.send(f"{user.name} has been sent to the shadow realm")
bot.run(Bot_Token)
keep_alive.py
The discord.Member class doesn't have a server attribute. user.guild.roles is what you most likely are looking for. https://discordpy.readthedocs.io/en/stable/api.html#discord.Member

How do i get embeds to work with discord.py?

So i am making a test command to for my bot and embeds are not work for me here is my code and i have tried looking it up but in my old bot i got it to work but not with cogs
import discord
from discord.ext import commands
class TestCog(commands.Cog, name="test command"):
def __init__(self, bot):
self.bot = bot
#commands.command(name = "test",
usage="",
description = "Testtttttt.")
#commands.cooldown(1, 2, commands.BucketType.member)
async def test(self, ctx):
embed=discord.Embed(title="Ping")
embed.add_field(name="Pong", value="undefined", inline=False)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(TestCog(bot))
You need to specify a description in your embed - if you don't want this to hold any value, then you can simply assign it "".
import discord
from discord.ext import commands
class TestCog(commands.Cog, name="test command"):
def __init__(self, bot):
self.bot = bot
#commands.command(name = "test",
usage="",
description = "Testtttttt.")
#commands.cooldown(1, 2, commands.BucketType.member)
async def test(self, ctx):
embed=discord.Embed(title="Ping", description="")
embed.add_field(name="Pong", value="undefined", inline=False)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(TestCog(bot))

#bot.command() doesn't run - No errors show up

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'))
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)
I'm trying to get my bot to respond "hi" when a user runs command "!hi". The problem is that the command doesn't run and no errors show up as well. The code within the .py file is exactly as it is shown (except TOKEN is replaced with an actual Token)
You're running client, and decorating the command with bot.command(), you have to either use discord.Client or commands.Bot not both.
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commmands.Bot(command_prefix=command.when_mentioned_or("!"), intents=intents)
#bot.command()
async def hi(ctx):
await ctx.send("Hi")
# You can also use events with `commands.Bot`
#bot.event
async def on_ready():
print("Ready")
bot.run("TOKEN")
The problem here is that you're using both discord.Client and commands.Bot
here's how to fix it:
import discord
from discord.ext import commands
TOKEN = "Your Token"
Intents=discord.Intents.all()
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), Intents=Intents)
#bot.command()
async def hi(ctx):
await ctx.send("hi")
client.run(TOKEN)

Resources