Home > Software design >  Discord bot cannot fetch channel
Discord bot cannot fetch channel

Time:01-26

I am trying to code a bot that will simultaneously print new messages in a server to console and for the user to be able to send messages to the server at the same time.

import discord
from asyncio import run
from threading import Thread

intents = discord.Intents.all()
intents.members = True
client = discord.Client(intents=intents)

async def p():
  ap = await client.fetch_channel(1234567890)
  return ap

main_guild = run(p())


def log_msg(msge,date):
  with open('log.txt','a') as f:
    f.write(msge   '\n'   date)
  f.close()

async def send_a_message():
  while True:
    await main_guild.send(input('Send message: '))


@client.event
async def on_message(message):
  base_msg = str(message.author) ': ' str(message.channel.name) ': ' str(message.content)
  date = str(message.created_at)
  if len(message.attachments) == 0:
    print(base_msg)
    log_msg(base_msg,date)
    return
  for i in range(len(message.attachments)):
    base_msg  = '\n' (message.attachments[i]).url
  log_msg(base_msg,date)
  
t = Thread(target=lambda:run(send_a_message()))
t.start()

try:
  client.run('#######################')
except discord.errors.HTTPException:
  from os import system
  system('kill 1')

However, I get a strange error:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    main_guild = run(p())
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "main.py", line 10, in p
    ap = await client.fetch_channel(999141783709679668)
  File "/home/runner/RandomRepl/venv/lib/python3.10/site-packages/discord/client.py", line 1824, in fetch_channel
    data = await self.http.get_channel(channel_id)
  File "/home/runner/RandomRepl/venv/lib/python3.10/site-packages/discord/http.py", line 604, in request
    if not self._global_over.is_set():
AttributeError: '_MissingSentinel' object has no attribute 'is_set'

What is causing this and how do I fix this?

Thank you.

CodePudding user response:

To get the channel you are using the client. But you are trying to get the channel before actually running the client, so you can't get the channel.

You have to first run the client and then get the channel. A possibility would be to use the on_ready event (called after client.run('...')) to get the channel.

Here is an example code you can start working with:

import discord

client = discord.Client(intents=discord.Intents.all())
main_guild = None

@client.event
async def on_ready():
    global main_guild
    await client.wait_until_ready()
    main_guild = client.get_channel(1234567890)
    print('Connected')

@client.event
async def on_message(message):
    if message.author.id == client.user.id: return

    if message.channel.type == discord.ChannelType.private: channel = 'DM'
    else: channel = message.channel.name

    base_msg = f'{message.author}: {channel}: {message.content}'
    await main_guild.send(base_msg)

client.run('#######################')
  • Related