0
votes

First of all I am sorry if I'm doing something wrong. This is my first question. I am currently trying to create a Discord Bot using Python.

Edit: Though the answer of one person helped me a lot, my question remains, because I still have the "await coro" exception and another one was thrown after I corrected the old mistake. I've updated the code and the exceptions. Thanks for your help!

When I'm trying to send an embed when the bot joins the server, I get two exceptions. Since I don't have 50 servers, I replaced the on_member_join(self) with a simple function call when something is written in a channel:

  1. File "...\Python\Python39\lib\site-packages\discord\client.py" await coro(*args, **kwargs)
  2. TypeError: on_message() missing 1 required positional argument: 'ctx'
    Though I watched videos and searched on stackoverflow, I still don't understand ctx completely. That's probably the reason I'm making this mistake. If you could help me correct the code or even explain what ctx is (is it like "this" in java?), that'd be great!

Here's the code of the two functions trying to send an embed:

import discord
from discord.utils import get
from discord.ext import commands

Bot_prefix = "<" #Later used before every command

class MyClient(discord.Client):

    async def Joining_Server(ctx, self):
        #Get channel by name:
        channel = get(ctx.guild.text_channels, name="Channel Name")

        #Get channel by ID:
        channels_Ids = get(ctx.guild.text_channels, id=discord.channel_id)

        embed = discord.Embed(title="Thanks for adding me!", description="Try")
        
        fields = [("Prefix", "My prefix is <. Just write it in front of every command!", True), 
                  ("Changing prefix", "Wanna change my prefix? Just write \"<change_prefix\" and the prefix you want, such as: \"<change_prefix !\"", True),
                  ("Commands", "For a list of the most important commands type \"<help\"", False),
                  ("Help", "For more help, type \"<help_All\" or visit:", True)]

        for channels in self.channel_Ids:
            if(commands.has_permissions(write=True)):
                channel_verified = channels.id 
        
        await ctx.channel_verified.send(embed)

    async def on_message(message, self, ctx):
        if message.author == client.user:
            return
        if message.content == "test":
            await MyClient.Joining_Server(ctx, self)

Thank you for helping me! Again: I'm sorry if I'm doing something wrong, it's my first question. Please ask if you need something. Feedback would also be very helpful.

1
Why are you creating your own client instance and then using commands.Bot inside of it? Why are you not putting self inside of the Joining_Server command? Shouldn't that also be a simple function instead of a command? Most of the code doesn't really make sense to meŁukasz Kwieciński
I'm sorry, it's my first real project and I sometimes don't really know what I'm doing to be honest. Thanks for your help!Exit Code 1

1 Answers

0
votes

I think you simply want to compare the content of the message to the "test" string

if message.author == client.user:
    return
if message.content == "test":
    await MyClient.Joining_Server()