'Join' Command Not Working - Need Assistance - discord.py

I saw many tutorials online to check my code, but none of them seems to help my problem. Here is my code for joining a voice channel:
#commands.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
I'm not making anything too fancy right now, I just want a way to get this to work. Thanks.

Here's my code for joining a vc
channel = ctx.message.author.voice.channel
if ctx.voice_client is not None:
return await ctx.voice_client.move_to(channel)
await channel.connect()
If the problem still persists, I need you to send more part of the code (Initialisation and Execution)

Related

Discord.py Server Lockdown Command

I would like to add a command to lockdown an entire server and revert the lockdown. It works, but it changed ALL the channels send messages permission to False/True. I'm asking if there is a way to set the permissions to neutral and not to change channel permissions if it is already false or true. Here is my current code:
#Lockdown
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for channel in ctx.guild.channels:
await channel.set_permissions(ctx.guild.default_role, send_messages=False)
await ctx.send('The server is now on lockdown!')
#unlockdown
#client.command()
#commands.has_permissions(manage_channels=True)
async def unlockdown(ctx):
for channel in ctx.guild.channels:
await channel.set_permissions(ctx.guild.default_role, send_messages=True)
await ctx.send('Server is now unlocked')
Yes you can, passing None will delete the overwrites for the channels, putting it back to normal.

How to delete all text & voice channels in a discord server with discord.py with one command?

I need to make a command to delete every channel (text and voice) in a discord server with discord.py. Can someone provide me with the code to do this. Right now i have this, which only deletes text channels. How do i make it for both at the same time?
#bot.command(name="deleteall")
async def delete_channels(context):
[await channel.delete()
for channel in context.guild.text_channels]
This seems like an oddly destructive command, you should be careful with its use and definitely add permissions checks.
To delete the voice channels, you'd just repeat the code you've already written, but just use the voice_channels list.
e.g.
[await channel.delete() for voiceChannel in context.guild.voice_channels]
#bot.command()
async def delchannels(ctx):
for c in ctx.guild.channels: # iterating through each guild channel
await c.delete()
You can also vary this specifically for text channels or voice channels using:
#bot.command()
async def delchannel(ctx, channel: discord.TextChannel):
await channel.delete()
And alter the argument type for specific channel types: "VoiceChannel" or "CategoryChannel".

Discord.py How to make the bot check every new message?

I'm using Discord.py how can I make the bot execute some operations when a user post a new message?
I highly recommend reading through the docs first before coming here to ask a question. However, you can use a client event and on_message to check every new message, for example:
#client.event
async def on_message(message):
if message.content == "Hi":
await message.channel.send("Hello!")

How do I get a bot to forward its DMs

So my bot DM's users and asks them questions, but I need to be able to see their response in a channel.
So far I have this:
#bot.event
async def on_message(message):
channel = bot.get_channel(642759168247463937)
await channel.send('message')
but it begins when any message is sent in any channel and it also responds to itself, causing an endless loop of spam.
I'm new to Discord.py so I honestly don't have a clue on how to resolve this issue.
My approach would be to check if the message came from DMs:
#bot.event
async def on_message(message):
if not message.guild:
return
channel = bot.get_channel(642759168247463937)
await channel.send('message')
This works because message.guild is None if the message was sent in DMs. This prevents the spam problem, since the message is not forwarded to the bot's DMs.
However, to avoid the spam in general, it is good practice to avoid responding to bots altogether, unless necessary. Luckily, User and Member objects have an attribute called bot that helps us here:
if message.author.bot:
return
Note that this is done automatically for commands using the ext.commands framework.

How can I make a bot change it's presence (listening, watching, playing, etc...) displayed on discord under their name?

I get nothing showing up that there is an error, but it does not show up in discord. Could someone show me what is wrong?
async def change_status(self):
await client.change_presence(game=Game(name = " ", type = 3))
I would like for the bot to have "listening" or "watching" show up on discord under it's name.
Okay, Update:
(pretty sure this is rewrite)
I figure some people will look this up over time so here it is.
On the discord.py discord server I looked around through #help and found a place where it said the correct answers they just needed to be edited slightly. Here is the answer for it:
await client.change_presence(activity=discord.Game(name="a game"))
This will set the bot to "Playing."
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))
This will set it to "Watching" status.
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))
This will set it to a "Listening" status.
You need the bot to specify when to change the status.
try #client.event this changes the status as the bot comes online.
to change listening to streaming or to watching try changing 2 ( 1,2,3,4 )
As far as i know to use the streaming status feature you need to link your twitch account with your code.
#client.event
async def on_ready():
await client.change_presence(game=Game(name='What ever in here',type = 2))
print('Ready')

Resources