#List all text channels in 2 lines or less

1 messages · Page 1 of 1 (latest)

thin zephyr
#

Is there anyway to list all text channels in a a small amount of code, all the example I have seen use for loops and I think there has to be a better way

little basin
#

list comprehension

storm flare
#

ctx.guild.text_channels is already a list

thin zephyr
calm haven
thin zephyr
lapis shell
#

example:

async def main(self, ctx):
  await ctx.respond(ctx.guild.text_channels)
  ...
#

also may have found a solution for this:

async def test(self, ctx):
  await ctx.respond([items.name for items in ctx.guild.text_channels])
thin zephyr
lapis shell
thin zephyr
#

huh I tried something like that but it keeps saying ctx is undefined

#

do I need to import anything else other than discord

thin zephyr
#

since I only have pycord imported

#

and asyncio and my keys file

rocky stag
#
async def on_message(message):
    await message.channel.send([items.name for items in message.guilds.text_channels])
#

This should prob work

rocky stag
thin zephyr
#

mainly since I need to run a function on startup for syncing channels

rocky stag
#

wdym by that

lapis shell
#

when the bot first starts up ?

thin zephyr
#

so this a discord to slack bridge, the bot get's a list of channels from both services and checks them

#

IE if one channels does not exist on slack it is added

#

or visa versa

rocky stag
#

what's slack

#

A list?

thin zephyr
#

slack is like work discord

lapis shell
#

just discord but for professionals

rocky stag
#

oh

thin zephyr
#

I already got most of the slack stuff worked out just the discord stuff is needed

lapis shell
#

so the way i would approach this is to create an asyncio task when the bot starts up, and within that task, constantly listen for any changes either discord's websocket gateway or slack's websocket gateway

#

there are many ways to do it, but this is how i would do it

thin zephyr
rocky stag
#

Also for this you would want to use an on_ready event

thin zephyr
#

so I would need to list all channels without using a bot command basically

rocky stag
#

Like-

@client.event
async def on_ready():
   #connect to your slack
#

Hmm

thin zephyr
#

I already run both using asyncio task

#

I just need a list of channels

rocky stag
#

Then in the on_ready function
Add this code-

guild = client.get_guild(yourguildid)
channels = guild.text_channels
#

So once your bot starts up it will automatically get the guild's channels for u

#

And channels var will store the all the channels

thin zephyr
#

Okay awesome