Как получить канал по id в discord.py?
Ну, начнем с того, что канал это часть сервера, что вам тоже нужно найти. Ну а дальше просто Клиент -> ПолучитьGuild(id_сервера) -> У объекта сервера ПолучитьКанал(id_канала). Методы думаю без проблем найдете сами. Некоторые библиотеки имеют сразу метод по типу GetChannel, который сразу дает нужное.
8 июл в 18:09
1 ответ 1
Сортировка: Сброс на вариант по умолчанию
Если вы хотите получить канал по его id при использовании команды, то можно воспользоваться методом get_channel() из класса Guild
from discord.ext import commands bot = commands.Bot() @bot.command() async def channel(ctx: commands.Context): channel_id = 1234567890 channel = ctx.guild.get_channel(channel_id)
Также, канал можно получить через функцию get из модуля utils . (P.S. Пример ниже ищет канал среди всех каналов серверов к которым бот подключён.)
import discord from discord.ext import commands bot = commands.Bot() channel_id = 1234567890 channel = discord.utils.get(bot.get_all_channels,
Is it possible to get channel ID by name in discord.py
The title says it all. I want to get the channel id given a specific name in a guild, but I could not find anything in the documentation that lets me do that. Looking at more developed bots like Mee6, they have the option to connect to your server and set a welcome page for any of your text channels. I’ve tried something like:
channels = discord.utils.get(client.get_all_channels(), guild__name='Test Server'
But this only returns ‘Text Channels’ and nothing else.
asked Aug 8, 2020 at 23:22
Hasnain Ali Hasnain Ali
432 1 1 gold badge 4 4 silver badges 13 13 bronze badges
I don’t understand. Is this running as part of a command? If you have the guild you can do get(guild.channels, name=»channel name»)
[Discord.py] How do I make a converter that returns either a member object or channel object to use in a command?
I have a .say command for my bot that accepts a channel parameter, and it sends whatever message you give it to that channel.
To pass the channel to the command, I have set up the following converter for text channels:
class get_channel(commands.TextChannelConverter): async def convert(self, ctx, argument): if argument: bot = ctx.bot mentioned = re.findall(r»», argument) # get the channel id if it’s a mention argument = mentioned[0] if mentioned else argument if argument.isdigit(): channel = bot.get_channel(int(argument)) else: guild = bot.get_guild(376981415633354753) channel = discord.utils.get(guild.text_channels, name=argument) return channel
I use it in my command like so:
async def say(self, ctx, channel: get_channel = None, *, message=None):
So I can give it either the channel ID, name, or mention, and they all work (e.g. .say 376981415633354755 or .say general or .say #general are all equally valid.
I can adapt the same pattern to work with members for a members converter, but I now would like to make a converter that lets me use both.
I have a different command that does sentiment analysis on a given set of messages, and those messages could either be the last 100 messages sent by a specific member, or the last 100 messages sent by anyone in a specific channel.
.analyse member , .analyse @member , @analyse
.analyse channel , .analyse #channel , @analyse
Should all be valid ways to use the same command.
I haven’t yet figured out how to combine the two converters neatly because what I tried so far started looking like a nested mess of if-else statements, which I scrapped.
Help would be appreciated. Thanks!