Why can't I remove embed without deleting the message? - python

I want to delete embed without deleting the message. In the documentation it is written that if embed = None, the embed will be deleted. I have: discord=1.7.3 and discord.py=1.7.3
But this did not work for me.
#commands.command()
async def lol(self, ctx):
embed = discord.Embed(title='Title', description='description', color=discord.Colour.gold())
embed.add_field(name='name', value=f'value', inline=False)
msg = await ctx.channel.send(embed=embed)
await asyncio.sleep(1)
await msg.edit(content='content', embed=None)

I found out what the problem was. I use the "discord_components" library to use the discord buttons. The problem only exists when "discord_components" is initialized.
import discord
import discord.ext
import asyncio
from discord.ext import commands, tasks
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
DiscordComponents(client)
print('----------Ready---------')
#client.command()
async def lol(ctx):
embed = discord.Embed(title='Title', description='description', color=discord.Colour.gold())
embed.add_field(name='name', value=f'value', inline=False)
msg = await ctx.channel.send(embed=embed)
await asyncio.sleep(5)
await msg.edit(content='content', embed=None, suppress=True)
client.run('***********************************************************')

Related

Can't run a join function because of a on_message event

i'm making a discord bot with python. I have some issues about running using #client.command and #client.event at the same time.
Here is the code:
when I comment the #client.event before the on message function, the join command run. This function cause a particular issue, do you know guys where it can come from? Thank you guys
import discord
import random
from discord.utils import get
from discord.ext import commands
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#client.command(pass_context=True)
async def leave(ctx):
await ctx.voice_client.disconnect()
#client.event
async def on_ready():
print("We have logged as {0.user}".format(client))
#client.event
async def on_message(message):
user = message.author.id
if message.content.lower() == "!poisson":
await message.delete()
with open('myimage.png', 'rb') as f:
picture = discord.File(f)
await message.channel.send(file=picture)
Put await client.process_commands(message) at the end of on_message()
If you're using on_message, then normal commands will be overridden unless you use process_commands.

Discord Python Bot unable to set prefix

Okay so I am designing a Discord bot in Python, am pretty new though. I was trying to get it to join a voice channel so I did some research and came up with the following, I cut out the irrelevant part of the bot so this is just the part with the voice connection. It says: undefined name "commands" over the line that should set the prefix:
import discord
import os
import requests
import json
import random
client = discord.Client()
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print("Eingeloggt als {0.user}".format(client))
#bot.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#bot.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run(os.getenv("TOKEN"))
You must use from discord.ext import commands
import discord
import os
import requests
import json
import random
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print("Eingeloggt als {0.user}".format(client))
#bot.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#bot.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
bot.run(os.getenv("TOKEN"))

Discord bot not connecting

Anybody know why my bot is not connecting to my discord voice channel?
import discord
import youtube_dl
from discord.ext import commands
import asyncio
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect
import discord
bot = commands.Bot(command_prefix="-")
#bot.command(pass_context=True)
async def join(ctx):
vc = ctx.author.voice
if vc:
await vc.channel.connect()
This should work.
You aren't calling the channel.connect method, plus channel.connect is a coroutine, and needs to be awaited, await channel.connect()

Discord.py Bot Not Playing music

Hi guys, for some reason my bot can actually join, but is not able to play audio.
Actually I am totally new to that and I was trying to get it to work like 2 hours and managed just to set up join but not play (or leave) command.
import os
import random
from dotenv import load_dotenv
import youtube_dl
import discord
from discord.ext import commands
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
players={}
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
#bot.command(name="Ja_som_to_nebol.")
async def sranda(ctx):
sranda_quotes = ["Not gonna lie, kinda sus",
("Not gonna lie, kinda sus"), ]
response = random.choice(sranda_quotes)
await ctx.send(response)
##bot.event
#async def on_command_error(ctx, error):
# if isinstance(error, commands.errors.CheckFailure):
# await ctx.send('You do not have the correct role for this command.')
#bot.command(name="join")
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#bot.command(name="leave")
async def leave(ctx):
channel = ctx.author.voice.channel
await channel.disconnect()
#bot.command(name="play")
async def play(ctx, url):
guild = ctx.author.voice.channel
voice_client = discord.utils.find(lambda c: c.guild.id == server.id, client.voice_client)
player = await voice_client.create_ytdl_player(url)
players=[server.id] = player
player.start()
bot.run(TOKEN)
instead of playing like this use my code which I uses you must have FFmpeg installed.
#commands.command(name='play', aliases=['p'])
async def _play(self, ctx: commands.Context, *, search: str):
if not ctx.voice_state.voice:
await ctx.invoke(self._join)
async with ctx.typing():
try:
source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
except YTDLError as e:
await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
else:
song = Song(source)
await ctx.voice_state.songs.put(song)
await ctx.send('Enqueued {}'.format(str(source)))
I am using some Cogs here you can edit to normal code by yourself.

Why bot.commands won't work in discord.py?

This is my code:
import discord
import asyncio
from discord import Game
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print(bot.user.id)
print("botstart")
game = discord.Game("abc")
await bot.change_presence(status=discord.Status.online, activity=game)
#bot.event
async def on_message(message):
if message.content.startswith("hi"):
await message.channel.send("Hello")
if message.content.startswith("ping"):
await message.channel.send("Pong")
#bot.command
async def ping(ctx):
ctx.send("Pong!")
bot.run("(my token)", bot=True)
For some reason, bot.event works but #bot.command(!ping) part doesn't. I tried to fix it but i couldn't, even watching discord.py guides.
What am I doing wrong?
Since you are overriding the on_message event, the bot no longer responds to any commands. To fix this add await bot.process_commands(message) to your on_message function for it to handle commands normally.
Source:
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Categories