Print the list of members in a voice channel - python-3.x

i'm coding a discord bot and I need a function that kick all members in my channel. I wrote this code:
#client.command()
async def separaci(ctx):
canale = ctx.message.author.voice.channel
utenti = canale.members #This return an empty list
for utente in utenti:
await utente.edit(voice_channel = None)
I don't know why canale.members return an empty list. Can you help me? Thanks you :)

Try this:
#client.command()
async def separaci(ctx):
if ctx.author.voice: # if the author is connected to a voice channel
canale = ctx.message.author.voice.channel
utenti = canale.members #This return an empty list
for utente in utenti:
await utente.edit(voice_channel = None)
await ctx.send("Kicked all the members from the voice channel!")
else:
await ctx.send("You need to be in a voice channel!")
return
NOTE:
You need to be in a voice channel while using this command.
Make sure the bot has the permission to disconnect the members present in the voice channel.
Make sure you have the members intent enabled in your developer portal.

You have to enable member intents, also make sure to enable them in the developer portal
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='', intents=intents)
#client.command()
async def separaci(ctx):
channel = ctx.author.voice.channel
members = channel.members
A Primer Gateway to Intents

Related

can't receive the message.content with discord bot

Hello I began a discord bot but I can't give the message's content. I managed to get all the other things about message but the content return nothing. here is the code(ps: sorry if my english isn't very good, i'm French) :
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def on_message(self, message: discord.message.Message):
# we do not want the bot to reply to itself
print(message.id)
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
print('ok')
await message.channel.send('Hello {0.author.mention}'.format(message))
defIntents = discord.Intents.default()
defIntents.members = True
client = MyClient(intents=defIntents)
client.run('token')
You have to enable the Message Content Intent of your application from Discord Developer Portal and in your code you have to set the message_content intent to True like this.
defIntents = discord.Intents.default()
defIntents.members = True
defIntents.message_content = True
client = MyClient(intents=defIntents)

D.py/rewrite - Confessions System

I made a confessions system but there’s some things that are wrong with it. How would I make it so when users want to type, they don’t have to put in *confess and they can just type whatever they want without needing to use a command? And how do I make a mod logs channel to log the deleted confessions with the author name, etc.?
import discord
from discord.ext import commands
class Confess(commands.Cog):
def __init__(self, client: discord.ext.commands.Bot):
self.client = client
#commands.command()
async def confess(self, ctx: commands.Context, *, message: str):
channel = self.client.get_channel(806649868314869760)
await ctx.message.delete()
embed = discord.Embed(title="Success", description=f"I've received your confession and sent it to the <#806649874379964487> channel!")
embed.set_footer(text="Confessions")
await ctx.send(embed=embed, delete_after=10)
channel = self.client.get_channel(806649874379964487)
embed = discord.Embed(title="Confession", description=f"{message}")
embed.set_footer(text="All confessions are anonymous.")
await channel.send(embed=embed)
def setup(client):
client.add_cog(Confess(client))
For the first question
If you want to use a "command" without actually using a command you could make an on_message event, check the id of the channel (like a confessions channel) and if it matches then do the thing
Example:
#commands.Cog.listener()
async def on_message(message):
if message.channel.id == some_channel_id_here:
channel = self.client.get_channel(806649868314869760)
await message.delete()
embed = discord.Embed(title="Success", description=f"I've received your confession and sent it to the <#806649874379964487> channel!")
embed.set_footer(text="Confessions")
await message.channel.send(embed=embed, delete_after=10)
channel = self.client.get_channel(806649874379964487)
embed = discord.Embed(title="Confession", description=f"{message}")
embed.set_footer(text="All confessions are anonymous.")
await channel.send(embed=embed)
For the second question
You can use get_channel again to get the log channel and post in there. (If you mean't on how to check if someone deleted a message/confession, use on_message_delete)
Example:
#commands.command()
async def confess(self, ctx: commands.Context, *, message: str):
channel = self.client.get_channel(806649868314869760)
log_channel = self.client.get_channel(log_channel_id)
await ctx.message.delete()
embed = discord.Embed(title="Success", description=f"I've received your confession and sent it to the <#806649874379964487> channel!")
embed.set_footer(text="Confessions")
await ctx.send(embed=embed, delete_after=10)
channel = self.client.get_channel(806649874379964487)
embed = discord.Embed(title="Confession", description=f"{message}")
embed.set_footer(text="All confessions are anonymous.")
await channel.send(embed=embed)
await logchannel.send("User confessed something!")

I want nc!nuke #channel for bot delete all messages (dont channel, only messages on channel)

whos have code on that maybe?
async def nuke(ctx, channel: discord.TextChannel = None):
if channel == None:
await ctx.send("You did not mention a channel!")
return
nuke_channel = discord.utils.get(ctx.guild.channels, name=channel.name)
if nuke_channel is not None:
new_channel = await nuke_channel.clone(reason="Has been Nuked!")
await nuke_channel.delete()
await new_channel.send("THIS CHANNEL HAS BEEN NUKED!")
await ctx.send("Nuked the Channel sucessfully!")
else:
await ctx.send(f"No channel named {channel.name} was found!")```
You will have to use the discord.TextChannel.purge() function.
channel # discord.TextChannel object (most likely the argument)
await channel.purge(limit=100) # Purge a certain amount of messages. You can choose
See the purge() function docs

Discord.py grabbing twitch URL without an API

I have a discord bot.
What I want it to do is when the bot detects a user is streaming, print the URL of their stream into chat.
I'm having a problem trying to find out how to get the user's twitch channel url...
this is what I have
#client.event
async def on_member_update(before, after):
aname = after.display_name
aactivity = after.activity.type
mactivity = str(after.activities)
role = after.guild.get_role(736271867836498031)
channel = client.get_channel(736269005651836985)
memberinfo = after.activities.streaming.url
print(memberinfo)
if "Streaming name" in mactivity:
await after.add_roles(role)
await channel.send('{} has started streaming!'.format(after.display_name))
print(aname + " is streaming!")
else:
await after.remove_roles(role)
streamchannel = discord.Profile
await channel.send('{} is not streaming!'.format(after.display_name))
print(aname + " is not streaming.")
print('member updated status')
Is there something I'm missing? I don't know how at all to either find the URL of the streamer, or get it from their connect accounts.
Member objects have a Activity attribute. You can check if before.activity or after.activity is an instance of the Streaming class.
If so, the user either stopped or started streaming and we can send a message in channel:
from discord import Streaming
from discord.utils import get
#client.event
async def on_member_update(before, after):
if not before.activity.type == after.activity.type:
return
role = get(after.guild.roles, id=736271867836498031)
channel = get(after.guild.channels, id=736269005651836985)
if isinstance(after.activity, Streaming):
await after.add_roles(role)
await channel.send(f"{before.mention} is streaming on {activity.platform}: {activity.name}.\nJoin here: {activity.url}")
elif isinstance(before.activity, Streaming):
await after.remove_roles(role)
await channel.send(f'{after.mention} is no longer streaming!')
else:
return
Reference: discord.py documentation

discord.ext.commands.errors.MissingPermissions: You are missing Mute Members permission(s) to run this command

I'm trying to program a command that mutes every member in the author's voice channel. I'm doing this so myself and my friends can mute ourselves when we swap to in-game voice chat automatically. But for reasons I cannot explain, I can never get it to work. Here's my code:
#commands.command()
#commands.has_permissions(mute_members=True)
async def mute(self, ctx):
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
for member in channel.members:
await member.edit(mute=True)
else:
await ctx.send("You are not connected to a voice channel!")
This is the full error
I understand that the bot and the author need a mute members permission, but both of them do! I even made sure that they were at the top of the role list, and I edited the voice channel permissions to allow mute members for the author and bot. No matter what I do, I always keep getting the same error! Any help would be greatly appreciated!
async def has_channel_permissions(**kwargs):
def predicate(ctx):
if ctx.author.voice is not None:
if ctx.author.voice.channel is not None:
if kwargs in dict(ctx.author.voice.channel.permissions_for(ctx.author)) and kwargs in dict(commands.user:
return True
else:
return False
await ctx.send("You are not connected to a voice channel!")
return False
return commands.check(predicate)
#commands.command()
#has_channel_permissions(mute_members=True)
async def mute(self, ctx):
channel = ctx.author.voice.channel
for member in channel.members:
await member.edit(mute=True)

Resources