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)
Related
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.
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'])
I'm making a bot for the discord server and I came up with the following code solution. The problem with this code is that anyone can use any command. I need to restrict the admin commands to users with the Admin role. I tried this but couldn't figure out what might be the right approach.
#Import
import discord
import os
from discord.ext import commands
#Client
client = commands.Bot(command_prefix='?')
client.remove_command('help')
#Activity
#client.event
async def on_ready():
print('Emog9 is running')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="?help"))
#Error
#client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.reply('Command does not exist. Please use a valid command')
#Cogs
#client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
#client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
Mute.py:
import discord
from discord.ext import commands
class mute(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
#commands.Cog.listener()
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRole("Administrator")):
return
async def mute(self, ctx, member: discord.Member, *, reason=None):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name='Muted')
if not mutedRole:
mutedRole = await guild.create_role(name='Muted')
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=True)
await member.add_roles(mutedRole, reason=reason)
await ctx.send(f'Muted {member.mention} for reason: {reason}')
await member.send(f"You were muted in the server {guild.name} for reason: {reason}")
def setup(client):
client.add_cog(mute(client))
Add the following decorator after the #client.command():
#commands.has_permissions(administrator=True)
You also can specify certain roles to use the command, add the following after the #client.command()
#commands.has_any_role('role_name', 'role_name')
Note that you can use the role id as an integer instead of the role name.
I use voice = await message.author.voice.channel.connect() to connect my bot to voice channel and i want the bot connect and deafen himself
All my code:
client = discord.Client()
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
arg = message.content.split(' ')
if message.content.startswith('>play'):
voice = await message.author.voice.channel.connect()
voice.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
await message.guild.change_voice_state(channel=voice, self_mute=True, self_deaf=False)
client.run('my token')
Use await message.guild.change_voice_state(channel=voice, self_mute=False, self_deaf=True)
Copy-paste this code that I wrote after looking at your comments:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='>')
#bot.event
async def on_ready():
print("Bot is ready!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
#bot.command()
async def join(ctx):
if ctx.author.voice is None or ctx.author.voice.channel is None:
return await ctx.send('You need to be in a voice channel to use this command!')
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
vc = await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
vc = ctx.voice_client
vc.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
await ctx.guild.change_voice_state(channel=vc, self_mute=False, self_deaf=True)
bot.run('my token')
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)