#๐Ÿ”’ Channel create: send msg

13 messages ยท Page 1 of 1 (latest)

cinder lantern
#

why does my code not send a message when i create channel
Main:

import asyncio
import os
import discord
from discord.ext import commands

intents = discord.Intents.all()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents, help_command=None)

my_secret = os.environ['Token']

@client.event
async def on_ready():
  print("\n\nBot is up!\n\n")
  for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
      client.load_extension(f"cogs.{filename[:-3]}")

my_secret = os.environ['Token']
client.run(my_secret)```
Cog:
```py
import discord
from discord.ext import commands

class MyCog(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_guild_channel_create(self, channel):
        """Event triggered when a new channel is created."""
        if channel.guild.system_channel:  # Check if the guild has a system channel
            await channel.guild.system_channel.send("Hello, world!")  # Send a message to the system channel

def setup(client):
    client.add_cog(MyCog(client))
pure urchinBOT
#

@cinder lantern

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rancid kernel
#

Do you get an error message or traceback?

cinder lantern
#

any idea?

rancid kernel
cinder lantern
# rancid kernel Show your main.py?

i did but here

import asyncio
import os
import discord
from discord.ext import commands

intents = discord.Intents.all()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents, help_command=None)

my_secret = os.environ['Token']

@client.event
async def on_ready():
  print("\n\nBot is up!\n\n")
  for filename in os.listdir("./cogs"):
    if filename.endswith(".py"):
      client.load_extension(f"cogs.{filename[:-3]}")

my_secret = os.environ['Token']
client.run(my_secret)```
rancid kernel
#

You need to call await client.load_extension

cinder lantern
# rancid kernel You need to call `await client.load_extension`

but now errror:

2024-03-17 19:31:09 ERROR    discord.client Ignoring exception in on_ready
Traceback (most recent call last):
  File "/home/runner/Receipt-Pro/.pythonlibs/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 947, in _load_from_module_spec
    await setup(self)
TypeError: object NoneType can't be used in 'await' expression

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Receipt-Pro/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/Receipt-Pro/main.py", line 17, in on_ready
    await client.load_extension(f"cogs.{filename[:-3]}")
  File "/home/runner/Receipt-Pro/.pythonlibs/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 1013, in load_extension
    await self._load_from_module_spec(spec, name)
  File "/home/runner/Receipt-Pro/.pythonlibs/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 952, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.cog1' raised an error: TypeError: object NoneType can't be used in 'await' expression
rancid kernel
#

Your def setup( needs to be async

cinder lantern
# rancid kernel Your `def setup(` needs to be async

you acc fixed it thanks man i got another question though how can i fix this so that it sends a message in the new channel if it starts with ticket-

import discord
from discord.ext import commands

class MyCog(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_guild_channel_create(self, channel):
        if channel.name == "ticket-"
            await channel.guild.send("Hello, world!")

async def setup(client):
    await client.add_cog(MyCog(client))
pure urchinBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.