Getting the discord voice channel using its id - discord.py

I had a necessity to connect to a voice channel using its id. After some research, I came across something like this:-
channel: discord.channel.VoiceChannel = ctx.message.author.voice.channel
But I am unable to find a way of getting the channel using its id.
Thanks

You can use the guild.get_channel method to get a voice channel by id.
voicechannel = guild.get_channel(123456789012345678)
If you don't already have the guild, you can get it from the message.
voicechannel = message.guild.get_channel(123456789012345678)

Related

discord.py - checking if channel id is correct

In Discord.py I am currently coding a bot that basically acts as a system for one particular server, and isn't intended to be used as a public bot. But I still wanted to add commands in that makes it simple for administrators to configure the bot inside of Discord, all was well, until I ran into the issue of trying to check if a channel ID is actually correct or not when executing the command, but unfortunately I kept getting error after error after error.
#bot.command()
async def channel(ctx, type, id):
global channel_report
global channel_approve
if id != discord.TextChannel.id:
await ctx.send("Command terminated: bad id.")
return
if type == "report":
#code here
elif type == "approve":
#code here
What have I been missing? I have tried to approach this many different ways, even with methods such as get_message , but got nowhere, and as a newish programmer, but especially new to the Discord API in particular, I'm lost. Thanks for all of those who are dedicating their time to help me, computer coding is just one of them interests.
I'm assuming what you mean by "checking if a channel ID is correct" is that you want to make sure the given ID represents an actual channel that the bot can access. You can use bot.get_channel() to get a channel object from an ID. If the channel doesn't exist, it returns null. So you can just check if it is null.
channel = bot.get_channel(000000000000000000) # try to get a channel which doesn't exist
if channel is None: # will return true as invalid channel returns null
await ctx.send("Command terminated: bad id.")
return

discord.py save every message in specific channel (using channel id)

How can a python bot read every message that is sent in a certain channel or read every message that was sent in one using a command like $save 421345187663708161. Thanks in advance, havent been able to find the answer anywhere.
I made this in an on_message Function which scans the message content for "$save" on the beginning, then gets the channel per the given ID.
#client.event
async def on_message(message):
if message.content.startswith("$save"):
splittedcontent = message.content.split()
channel = client.get_channel(splittedcontent[1])
all_messages = channel.history()
What your job now is, is to understand this code, maybe inform about the things that are going on here (like channel.history) and implement this to your code (preferably also with some try/except cases).

Fetch the message sent by the bot before restarting the session

I know that I can get a message object using await ctx.fetch_message(mesId). Although If I send a message, and then start the bot's session (Restart the Client). The script cannot see the message. Is there any way to get rid of this problem?
Also, it's worth mentioning that I use discord.Bot type user not discord.Client
First of all there is no ctx.fetch_message ref
It should be ctx.channel.fetch_message Keep in mind you can get another channel by using await bot.get_channel(ID) and then fetch the message.
Your code should look like this:
# using another channel
channel = await bot.get_channel(123456)
message = await channel.fetch_message(123456)
# or using ctx
message = await ctx.channel.fetch_message(123456)
Docs

discord.py how do I make my bot join a specific voice channel?

as the title suggests, I am trying to make my bot join a specific voice channel. I found other questions relating to how to make the bot join the authors voice channel. What I am trying to make is, join "x" voice channel. How come I do that?
Get the discord.Channel instance of the channel, and call it's connect() function.
channel_id = the_id_of_the_channel_you_want_to_join
voice_channel = client.get_channel(channel_id)
await voice_channel.connect()

How to determine a channel is a DM or a normal channel

I got a bot which may:
start talk when user say somethibng in DM with the bot.
start talk only when user mentioned the bot in some channel.
Question:
How can I know it's a DM channel or a normal channel?
I guess https://api.slack.com/methods/channels.info might help, but I'm not sure which property indicates it.
Your best approach is to use the conversations.info API method. It will tell you exactly what kind of channel it is. It actually replaces the older channels.info method (which only works for public channels anyways).
Among other you get:
"is_channel": true
"is_group": false
"is_im": false
Here is how those terms are used in the API and (mostly) in the documentation:
channel = public channel
group = private channel
im = direct message.
conversation = any kind of channel
Alternatively you can also guess the channel type based on the first letter of it's ID. That is not 100% though, because channels can be converted from one type to another while keeping their ID.
C: public channel
G: private chanel / MPIM
D: Direct Message / IM

Resources