Skip to content
Advertisement

Discord.py client.run and bot.run in one code

Not sure that I need to use the both runs at the same time, but:

from multiprocessing.dummy.connection import Client
from telnetlib import DM
from typing_extensions import Required
import discord
from discord.utils import get
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
from dislash.interactions import *

client = discord.Client()

from message import *
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    User_id = message.author.id
    
    if message.channel.id == 1009530463108476968:
        NewMessage = message.content.split(' ', 1)[0]
        LimitLenght = len(NewMessage) + 11
        
        if len(message.clean_content) >= LimitLenght:
            await message.delete()
            await message.author.send("Hello, " + f"<@{User_id}>" +  "nPlease, don't send any messages that break the **Counting-game** rules!nIt's forbidden to post a comment that is longer than 10 characters.")

        # More code

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#

bot = commands.Bot(command_prefix="!")
inter_client = InteractionClient(bot)
@inter_client.slash_command(name="help", description="Shows the help-menu")

async def help(ctx):
    embedVar = discord.Embed(title="Test project", description="*The blue text is clickable.*n⠀", color=0x0000ff)
    embedVar.add_field(name="Rules", value='To see rules write **/Rules**n⠀', inline=False)
    await ctx.reply(embed=embedVar, delete_after=180)


bot.run('ToKeN')
client.run('ToKeN')

If you run this code and comment the “bot.run(‘ToKeN’)”, the first part of the code will work (def on_message), however the command ‘/help’ will not work. If you change it (comment ‘client.run(‘ToKeN’)’), the command ‘/help’ will work, but def on_message not.

What are the possible solutions? Thanks.

Advertisement

Answer

The .runs block each other and prevent from running. You shouldn’t be using a client and a bot anyway. Use one commands.Bot instead. It subclasses a Client and it should be able to do everything you can do with the client.

bot = commands.Bot(command_prefix="!")
inter_client = InteractionClient(bot)

@bot.event
async def on_message(message):
    ...

@inter_client.slash_command(name="help", description="Shows the help-menu")
async def help(ctx):
    ...

bot.run(token)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement