So I was making a bot in discord.py which joins a voice channel and disconnects from it after a delay of 3 seconds, just to test how to make it disconnect. I have done much research but couldn't find it. The bot successfully connects but isn't able to disconnect. Here is the code-
if message.content == "-p":
channel = message.author.voice.channel
await channel.connect()
time.sleep(3)
await channel.disconnect()
And here is the error I get-
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\arnha\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\arnha\Desktop\Music bot\bot.py", line 44, in on_message
await channel.disconnect()
AttributeError: 'VoiceChannel' object has no attribute 'disconnect'
Kindly help me with the same.
It is pretty unintuitive. You have to use disconnect() on the voice_client that is an attribute of a guild. Like this:
channel = message.author.voice.channel
await channel.connect()
time.sleep(3)
voice_client = message.guild.voice_client
await voice_client.disconnect()
Related
I wan, that my discord.py bot delete a message, if I answer p!delete to a message.
I use this:
if message.content.lower() == 'p!delete':
message = message.reference
await message.delete()
and get this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "FILEPATH", line 303, in on_message
await message.delete()
AttributeError: 'MessageReference' object has no attribute 'delete'
the problem is you're overwriting the message object. Here is the improved code
if message.content.lower() == 'p!delete':
await message.reference.delete()
Thank you all, but it now works with this code:
deleteMessage = message.reference.message_id
deleteMessage = await message.channel.fetch_message(deleteMessage)
await deleteMessage.delete()
await message.delete()
I have started making Ticket bot using discord.py but I have an error in my code and I don't know how to solve it. Embed and reacting to message work but when I try to react everythings crashes. Here is my error:
Ignoring exception in command ticket:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 150, in ticket
if reaction == '📩':
NameError: name 'reaction' is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'reaction' is not defined
I hope someone have and idea how to fix this, here is my current code:
import discord
from discord.ext import commands
from discord.utils import get
from discord import Embed, Color
import DiscordUtils
import os
from discord.ext.commands import has_permissions, MissingPermissions
import json
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = "-", intents = intents)
client.remove_command("help")
#client.command(pass_context=True)
async def ticket(ctx):
guild = ctx.guild
embed = discord.Embed(
title = 'Ticket system',
description = 'React 📩 to make a ticket.',
color = 0
)
embed.set_footer(text="ticket system")
msg = await ctx.send(embed=embed)
await msg.add_reaction("📩")
reaction = await msg.fetch_message(msg.id)
await client.wait_for("reaction_add")
await client.wait_for("reaction_add")
if reaction == '📩':
await guild.create_text_channel(name=f'ticket - {reaction.author.name}')
ps: I am new to discord.py
reaction = await msg.fetch_message(msg.id) is just giving you msg. You instead need to take the reaction from await client.wait_for("reaction_add")
Take a look at wait_for. You implement a check (Which is where the reaction is) to see if the reaction is a mailbox.
You need to create a new function that creates a channel if the reaction is a mailbox, then pass that into client.wait_for.
def check(reaction, user):
return str(reaction) == '📩' and ctx.author == user
await client.wait_for("reaction_add", check=check)
await guild.create_text_channel(name=f'ticket - {ctx.author}')
You didn't define reaction, that's why this error is raised.
Take a look at this post to find out how to fetch reactions from messages.
I'm trying to program a discord bot with discord.py, the main goal right now is to have my bot join a voice channel and play an audio file that's on my laptop.
#client.command()
async def sing(ctx):
user = ctx.author.voice
if user != None:
channel_chat = ctx.author.voice.channel
await channel_chat.connect()
player = discord.FFmpegPCMAudio(executable="C:/Path_Program/ffmpeg.exe", source=r"C:\Users\ialwa\Desktop\Misc\Music\Zelda's Lullaby Ancient Tune Hyrule Warriors Age of Calamity Soundtrack.mp3")
player.start()
else:
await ctx.send("Beep beep! (Be in a voice channel please!)")
This is the block of code that's suppose to play a file, the bot joins the channel just fine but fails to play audio. I do have ffmpeg-python installed and already tried installing ffmpeg but both or one at a time brings the same error.
The error itself in full is here
Ignoring exception in command sing:
Traceback (most recent call last):
File "C:\Users\ialwa\PycharmProjects\TY\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\ialwa\PycharmProjects\TY\main.py", line 29, in sing
player.start()
AttributeError: 'FFmpegPCMAudio' object has no attribute 'start'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ialwa\PycharmProjects\TY\venv\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\ialwa\PycharmProjects\TY\venv\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\ialwa\PycharmProjects\TY\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'FFmpegPCMAudio' object has no attribute 'start'
If anyone can help me understand what to do to fix this problem it would be appreciated!
discord.FFmpegPCMAudio isn't a player, it's just an audio source.
In order to play audio files, you need discord.VoiceClient.play:
#client.command()
async def sing(ctx):
user = ctx.author.voice
if user is not None:
channel = ctx.author.voice.channel
voice = await channel.connect()
voice.play(discord.FFmpegPCMAudio(executable="C:/Path_Program/ffmpeg.exe", source=r"C:\Users\ialwa\Desktop\Misc\Music\Zelda's Lullaby Ancient Tune Hyrule Warriors Age of Calamity Soundtrack.mp3"))
else:
await ctx.send("Beep beep! (Be in a voice channel please!)")
how do I check whether a video file was sent to a discord server?
I tried using
#client.event
async def on_message(message): # Event handler for income messages
if message.author == client.user:
return None
response = 'https://cdn.discordapp.com/attachments/690944063888687114/724771151041265704/thats_my_meme_now.mp4'
if video in message.content:
await message.channel.send(
response)
but that gives me the following error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\seanb\Desktop\python projects\discord bot\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "C:/Users/seanb/Desktop/python projects/discord bot/discord bot.py", line 21, in on_message
if video in message.content:
NameError: name 'video' is not defined
This code loops through all video formats and checks if any of the attached files is in any of these formats.
#client.event
async def on_message(message):
if message.author.bot:
return
for a in message.attachments:
for e in ['3g2','3gp','amv','asf','avi','drc','f4a','f4b','f4p','f4v','flv','gif','gifv','m2ts','m2v','m4p','m4v','mkv','mng','mov','mp2','mp4','mpe','mpeg','mpg','mpv','mts','mxf','nsv','ogg','ogv','qt','rm','rmvb','roq','svi','ts','vob','webm','wmv','yuv']:
if a.filename[-len(e)-1:]==f'.{e}':
await message.channel.send('https://cdn.discordapp.com/attachments/690944063888687114/724771151041265704/thats_my_meme_now.mp4')
return
I made a bot for discord and it works fine when I launch it in local.
I build it on heroku and It's seems to work fine too (thanks to Tristo).
But in the log I get the following message :
2019-01-01T23:06:50.131982+00:00 app[worker.1]: Ignoring exception in on_message
2019-01-01T23:06:50.132550+00:00 app[worker.1]: Traceback (most recent call last):
2019-01-01T23:06:50.132589+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 227, in _run_event
2019-01-01T23:06:50.132590+00:00 app[worker.1]: await coro(*args, **kwargs)
2019-01-01T23:06:50.132593+00:00 app[worker.1]: File "run.py", line 14, in on_message
2019-01-01T23:06:50.132594+00:00 app[worker.1]: await client.send_message(message.channel, newMessage)
2019-01-01T23:06:50.132616+00:00 app[worker.1]: AttributeError: 'Bot' object has no attribute 'send_message'
My programm is :
from discord.ext.commands import Bot
import os
BOT_PREFIX = ("?")
access_token= os.environ["ACCESS_TOKEN"]
client = Bot(command_prefix=BOT_PREFIX)
#client.event
async def on_message(message):
if message.content.startswith("?"):
newMessage = 'text' + str(message.content)[1:].upper() + '.png'
await client.send_message(message.channel, newMessage)
client.run(access_token)
My requirements.txt includes only "git+https://github.com/Rapptz/discord.py#rewrite#egg=discord.py[voice]"
I tried to use "send" instead of "send_message" (the answer in a similar post) but nothing changes.
My bot seems to work despite the attribute message error.
Could you help me to understand what happened, please ?
You've installed the rewrite branch of discord.py that does sending messages in a different way
# before
await client.send_message(channel, 'Hello')
# after
await channel.send('Hello')
await message.channel.send('Hello') is what you need!