Discord bot not connecting - python

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()

Related

Discord.py bot won't leave a voice chat

i'm currently using these commands to make my bot leave/join a vc. Joining works fine, but leaving won't work.
import discord
from discord.ext import commands
import youtube_dl
class music(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You're not in a voice channel!")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_channel.move_to(voice_channel)
#commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
Any help in understanding why would be really appreciated!
try with:
#commands.command()
async def disconnect(self,arg):
await arg.voice_client.disconnect(force=True)
and also replace channel name from ctx to arg cause it takes ctx as current channel (its not possible to type in voice channel afaik)

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

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('***********************************************************')

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"))

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

my commands dont work in my discord server

im trying to make a bot for my discord server, but all my commands dont work.
im using windows and pycharm to test and use the bot. i tried many different types but nothing works. im using python 3.7
import discord
from discord.ext import commands
import asyncio
from discord.ext.commands import Bot
Client = discord.Client()
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print("bot is active")
#client.command()
async def ping(ctx):
await ctx.send('pong')
await print("pong")
it doesn't crash or give an error, it just doesn't do anything in the command
Try this:
import discord
from discord.ext import commands
TOKEN = YOUR_TOKEN_GOES_HERE
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Powering up the bot")
#client.event
async def on_message(message):
print("A new message!")
await client.process_commands(message)
#client.command()
async def ping(ctx):
await ctx.channel.send("Pong!")

Categories