Discord bot won't mute all people - python

It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?
#bot.command()
async def mute(ctx):
voice_client = ctx.guild.voice_client #
if not voice_client:
return
channel = voice_client.channel
people = channel.members
for person in people:
if person == client.user:
continue
await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
edit Full code:
import discord
from discord.ext import commands
import os
from discord.utils import get
client = discord.Client()
DISCORD_TOKEN = os.getenv("TokenGoesHere")
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 disconnect(ctx):
channel = ctx.message.author.voice.channel
await channel.disconnect()
#bot.command()
#commands.has_permissions(administrator=True)
async def mute(ctx):
voice_client = ctx.guild.voice_client #get bot's current voice connection in this guild
if not voice_client: #if no connection...
return #probably should add some kind of message for the users to know why nothing is happening here, like ctx.send("I'm not in any voice channel...")
channel = voice_client.channel #get the voice channel of the voice connection
people = channel.members #get the members in the channel
for person in people: #loop over those members
if person == client.user: #if the person being checked is the bot...
continue #skip this iteration, to avoid muting the bot
await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
#edit the person to be muted, with a reason that shows in the audit log who invoked this command. Line is awaited because it involves sending commands ("mute this user") to the server then waiting for a response.
#bot.command()
#commands.has_permissions(administrator=True)
async def unmute(ctx):
voice_client = ctx.guild.voice_client
if not voice_client:
return
channel = voice_client.channel
people = channel.members
for person in people:
if person == client.user:
continue
await person.edit(mute=False, reason="{} told me to mute everyone in this channel".format(ctx.author))
bot.run("TokenGoesHere")
Hope this helps, Bot sometimes mutes only itself or only itself and other user, but specifically that one user...
It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?

You have to define the intents to use some events, functions like on_message, guild.members, channel.members etc.
# Here is your imports
import discord
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='', intents=intents)
# Rest of your code
Also, you have to activate intents from Discord Developer Portal.
Go to your bot application.
Go to Bot -> Privileged Gateway Intents.
Activate Presence Intent and Server Members Intent.

Related

Discord.py I want to ping a certain role at every server my bot is in

So i want to ping #Wafaduck Alerts in every server my bot is in. When I input my command on this specific server I can see the ping but when it's on a different server it shows #deleted role.
heres my code
#client.command() #v1
#commands.has_permissions(administrator=True)
async def ping(ctx, *, msg):
role = get(ctx.guild.roles, name = 'Wafaduck Alerts')
for guild in client.guilds:
for channel in guild.channels:
if(channel.name == 'wafaduck-alerts'):
await channel.send(f"{role.mention}")
Make sure to get the role from each iteration rather than from the command. As Malware also stated make sure you create your own permissions handling otherwise someone would be able to ping all the admins.
#client.command() #v1
#commands.has_permissions(administrator=True)
async def ping(ctx, *, msg):
for guild in client.guilds:
role = get(guild.roles, name = 'Wafaduck Alerts')
for channel in guild.channels:
if(channel.name == 'wafaduck-alerts'):
await channel.send(f"{role.mention}")

Discord.py bot won't join voice

I'm trying to make a discord bot. It works fine for responding to certain messages, which is part of what I wanted it to do. I now want it to be able to join the voice channel, but it wont:
import discord
import youtube_dl
from discord.ext import commands
from discord.utils import get
TOKEN = 'token string here
BOT_PREFIX = '/'
bot = commands.Bot(command_prefix=BOT_PREFIX)
players = {}
#bot.event
async def on_ready():
print(bot.user.name + ' is now online.\n')
and then after my message handling #bot.event,
#bot.command(pass_context=True)
async def join(ctx):
print('Join executed')
global voice
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print (f"The bot has joined {channel}\n")
#bot.command(pass_context=True)
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
print(f"The bot has left {channel}")
else:
print("Bot was asked to leave, but was not in one.")
I'm very new, so it could be (or probably is) a very simple, stupid fix.
The join command isn't even being processed, as that print statement isn't showing up in console when I call it in the discord chat.
I was following this video tutorial to a T, and his clearly works fine.
I think you just got on_message event. If you do, check this docs page.
Also i have no idea why you passed pass_context they removed it at version 1.0, and it doesn't even exist now.
Check below. This will work with the latest version of discord.py.
#bot.command(name='join', invoke_without_subcommand=True)
async def join(ctx):
destination = ctx.author.voice.channel
if ctx.voice_state.voice:
await ctx.voice_state.voice.move_to(destination)
return
ctx.voice_state.voice = await destination.connect()
await ctx.send(f"Joined {ctx.author.voice.channel} Voice Channel")

Verification using Python Discord

I'm making a bot with python and I need help with two things.
Making a welcome message for users that include mentioning the user and mentioning the channel
Making a command that will remove the role "Unverified" and add 4 other roles. I also need it to send a message in the verification channel to make sure the person has been verified and send an embed in general chat telling the user to get self roles.
Well you could try
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix=".")
confirmEmoji = '\U00002705'
#client.event()
async def on_ready():
print("[Status] Ready")
#client.event()
async def on_member_join(ctx, member):
channel = get(ctx.guild.channels,name="Welcome")
await channel.send(f"{member.mention} has joined")
#client.command()
async def ConfirmMessage(ctx):
global confirmEmoji
message = await ctx.send("Confirm")
await message.add_reaction(emoji=confirmEmoji)
def check(reaction, user):
if reaction.emoji == confirmEmoji:
return True
else:
return False
while True:
try:
reaction, user = await client.wait_for("reaction_add", check=check, timeout=10)
roleToRemove = get(ctx.guild.roles,name="unverified")
memberToRemoveRole = get(ctx.guild.members,name=user.display_name)
await memberToRemoveRole.remove_roles(roleToRemove)
Now all you have to do is go to the channel and enter .ConfirmMessage

How to discord invite link with DIscord.py

is there a way i can create a invite link using Discord.PY? My code is the following/
import discord
from discord.ext import commands
import pickle
client = commands.Bot("-")
#client.event
async def on_message(message):
message.content.lower()
if message.author == client.user:
return
#checks if the bot it running.
if message.content.startswith("message"):
await message.channel.send("hello there")
#tells the user where they are.
if message.content.startswith("whereami"):
await message.channel.send(f"You are in {message.guild.name} " + \
f"in the {message.channel.mention} channel!")
##Creates Instant Invite
if message.content.startswith("createinvite"):
await create_invite(*, reason=None, **fields)
await message.channel.send("Here is an instant invite to your server: " + link)
client.run('token')
If needed, let me know what other information you need, and if i need to edit this to make it more clear. If I need to import anything else, please inform me as to what libraries are needed.
#client.event
async def on_message(message):
if message.content.lower().startswith("createinvite"):
invite = await message.channel.create_invite()
await message.channel.send(f"Here's your invite: {invite}")
And using command decorators:
#client.command()
async def createinvite(ctx):
invite = await ctx.channel.create_invite()
await ctx.send(f"Here's your invite: {invite}")
References:
TextChannel.create_invite()
discord.Invite - Returned from the coroutine.
Is there a way to create one, but only with one use?
i have an on_message event: if someone type xy, the bot will kick him. and after the kick i want to send him an xy message.(its ready)
and after this i want to send him an invite

Discord Python Bot: How to move specific users mentioned by the author of the message

I am looking for a way to allow a user to move him or her self and another user to a different voice channel. I already got the command to work for the author of the message, but I am having trouble finding out a way to move another user in the same message. The idea is that the user would be able to type "n!negotiate [Other User]" and it would move the author and the other user to the Negotiation channel.
I would love some help with how I might be able to do this. The code is provided below excluding the tokens and ids.
Code:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client() #Initialise Client
client = commands.Bot(command_prefix = "n!") #Initialise client bot and prefix
#client.event
async def on_ready():
print("Logged in as:")
print(client.user.name)
print("ID:")
print(client.user.id)
print("Ready to use!")
#client.event
async def on_message(check): #Bot verification command.
if check.author == client.user:
return
elif check.content.startswith("n!check"):
await client.send_message(check.channel, "Nations Bot is online and well!")
async def on_message(negotiation): #Negotiate command. Allows users to move themselves and other users to the Negotiation voice channel.
if negotiation.author == client.user:
return
elif negotiation.content.startswith("n!negotiate"):
author = negotiation.author
voice_channel = client.get_channel('CHANNELID')
await client.move_member(author, voice_channel)
client.run("TOKEN")
You should use discord.ext.commands. You're importing it, but not actually using any of the features.
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix = "n!") #Initialize bot with prefix
#bot.command(pass_context=True)
async def check(ctx):
await bot.say("Nations Bot is online and well!")
#bot.command(pass_context=True)
async def negotiate(ctx, member: discord.Member):
voice_channel = bot.get_channel('channel_id')
author = ctx.message.author
await bot.move_member(author, voice_channel)
await bot.move_member(member, voice_channel)
bot.run('TOKEN')
Here we use a converter to accept a Member as input. Then we resolve the author of the message from the invocation context and move both Members to the voice channel.

Categories

Resources