discord.py автоматическая отправка результата в discord в виде прямого сообщения

#python #if-statement #conditional-statements #discord #discord.py

#python #if-оператор #условные операторы #Discord #discord.py

Вопрос:

Я хотел бы знать, как я мог бы заставить этот скрипт отправлять результат (выигранный или проигранный) в прямом сообщении конкретного пользователя, вот простой пример этого:

     from discord.ext import commands
    import discord
    import os
    from random import *
    
    client = commands.Bot(command_prefix = '-')
    
    @client.event
    async def on_ready():
      print('Bot Is Ready')
    
    @client.command() #The command in order to execute the script first
    async def dm(ctx):
        
        rand_num = (randint(1, 3))
        win_num = 1
        
        if rand_num == win_num:
            print("number was:", rand_num)
            print("won")
            @client.event
            async def on_win():
                dmessage.send.user("You won!") #Send the won result message via direct message on discord automatically
    
        elif rand_num != win_num:
            print("number was:", rand_num)
            print("lost")
            @client.event
            async def on_lost():
                dmessage.send.user("You lost") #Send the lost result message via direct message on discord automatically
    
    client.run('TOKEN')
  

Ответ №1:

Если вы хотите отправить привилегированное сообщение(прямое сообщение), вы можете использовать member.create_dm и вы не можете использовать что-то вроде on_win или on_lost .

 @client.command()
async def dm(ctx):
    rand_num = (randint(1, 3))
    win_num = 1
    pm_channel = await ctx.author.create_dm()
    if win_num == rand_num:
        await pm_channel.send("You won!")
    else:
        await pm_channel.send("You lost")
  

Итак, в этом коде member при записи member prefix dm проверяет rand_num и win_num затем отправляет результат пользователю.