I just started learning python recently and never coded a discord bot before. I've been trying to get the bot to connect to the voice or respond to ">hello", but it doesn't respond to my command and I don't know how to fix it. I'm using replit as hosting.
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '>', intents=intents)
#client.event
async def on_ready():
print('Log in as {0.user}'.format(client))
print('-------------------')
#client.command()
async def hello(ctx):
await ctx.send("Hello Im Deemo Bot")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
await channel.connect()
else:
await ctx.send('You are not in a voice channel')
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send('Deemo left voice')
else:
await ctx.send('Deemo is not in voice')
keep_alive()
client.run(os.environ['TOKEN'])
Related
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import requests
import json
from apikeys import *
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '!', intents=intents)
#client.event
async def on_ready():
print("The bot is now ready for use!")
print("-----------------------------")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voice = await channel.connect()
source = FFmpegPCMAudio("song.wav")
player = voice.play(source)
else:
await ctx.send("You are not in a voice channel")
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send("I left the voice channel")
else:
await ctx.send("I am not in a voice channel")
client.run(BOTTOKEN)
Hey! I have recently started watching a tutorial series.
It works for the tutorial maker, but doesn't for me.
The bot is able to join the voice channel the user is in, but never starts playing "song.wav".
Sadly Visual Studio Code also doesn't output any Error messages.
Does anyone know a solution?
So I want to make a bot for my discord server so it joins a voice channel I am currently in and mutes all member and unmutes them on command, like vcmute and vcunmute, but somethings missing and nothing is working. here is the code I wrote
import discord
from discord.ext import commands
import os
client = discord.Client()
DISCORD_TOKEN = os.getenv("myToken")
bot = commands.Bot(command_prefix="$")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#bot.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#bot.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
#bot.command()
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
bot.run("myToken")
The code works for me, the bot is probably missing admin permissions which prevents it from muting and unmuting members.
You could also add the #commands.has_permissions() decorator to have it check for admin permissions, example for your code:
#bot.command()
#commands.has_permissions(administrator=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#bot.command()
#commands.has_permissions(administrator=True)
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
#bot.command()
#commands.has_permissions(administrator=True)
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
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.
this is my code rn:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="-")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#client.command()
async def hello(ctx):
await ctx.send("hei this is a test just dont mind me")
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
client.run('mytoken')
how do i mute everyone in that voice channel?
Iterate through all members of the voice channel and pass mute=True to the edit function.
#client.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
I'm trying to connect my discord bot to a voice channel, but it's not working.
There isn't any error or anything, nothing happens when I do !join on my discord channel.
Here is my code. I tried looking for some tutorials, but most seems outdated.
Could someone help me?
import discord
from discord.ext import commands
from discord.utils import get
import os
token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f'Joined {channel}')
client.run(token)
Edit:
import discord
from discord.ext import commands
from discord.utils import get
import os
import youtube_dl
token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')
#client.event
async def on_message(message):
channels = ['bot-commands']
if str(message.channel) in channels:
if message.content == '!help':
embed = discord.Embed(title='How to use the ImposterBot', description='Useful Commands')
embed.add_field(name='!help', value='Display all the commands')
embed.add_field(name='!music', value='Play a music playlist')
embed.add_field(name='!valorant', value='Get the most recent version of Valorant stats')
embed.add_field(name='!hello', value='Greet a user')
await message.channel.send(content=None, embed=embed)
elif message.content == '!hello':
await message.channel.send(f'Greeting, {message.author}!')
elif message.content == '!valorant':
await message.channel.send('This feature is not ready yet')
elif message.content == '!music':
await message.channel.send('This feature is not ready yet')
elif 'sustin' in message.content:
await message.channel.send(f"""it's sustin... {"<:monkas:392806765789446144>"} """)
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f'Joined {channel}')
client.run(token)
The error in this code is that commands will not be invoked by the bot if there is an on_message event, unless that event begins with this line:
async def on_message(message):
await bot.process_commands(message)