【问题标题】:How to create to step commands and how to send privet messages using discord.py如何创建步骤命令以及如何使用 discord.py 发送私人消息
【发布时间】:2021-05-06 20:48:47
【问题描述】:

我一直在开发一个不和谐的机器人,当你编写“/encryptkey 消息”时,它会将其转换为 Mors 代码使用解密的代码(iv 已经有一个有价值的名为 last_crypt 它是解密的最后一条消息) 问题: 1-不发送直接消息 2. 认为 /decryptkey 需要在同一条消息上 这是代码:

import discord
from discord.ext import commands

MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
                    'C':'-.-.', 'D':'-..', 'E':'.',
                    'F':'..-.', 'G':'--.', 'H':'....',
                    'I':'..', 'J':'.---', 'K':'-.-',
                    'L':'.-..', 'M':'--', 'N':'-.',
                    'O':'---', 'P':'.--.', 'Q':'--.-',
                    'R':'.-.', 'S':'...', 'T':'-',
                    'U':'..-', 'V':'...-', 'W':'.--',
                    'X':'-..-', 'Y':'-.--', 'Z':'--..',
                    '1':'.----', '2':'..---', '3':'...--',
                    '4':'....-', '5':'.....', '6':'-....',
                    '7':'--...', '8':'---..', '9':'----.',
                    '0':'-----', ', ':'--..--', '.':'.-.-.-',
                    '?':'..--..', '/':'-..-.', '-':'-....-',
                    '(':'-.--.', ')':'-.--.-'}

def encrypt(message):
    cipher = ''
    for letter in message:
        if letter != ' ':
  
            cipher += MORSE_CODE_DICT[letter] + ' '
        else:
            cipher += ' '
  
    return cipher
  
def decrypt(message):
  
    message += ' '
  
    decipher = ''
    citext = ''
    for letter in message:
  
        if (letter != ' '):
  
            i = 0
  
            citext += letter
  
        else:
            i += 1
  
            if i == 2 :
  
                decipher += ' '
            else:
  
                decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
                .values()).index(citext)]
                citext = ''
  
    return decipher


class MyClient(discord.Client):

    async def on_ready(self):
        print('Logged on as', self.user)

    async def on_message(self, message): 
        word_list = ['/encryptkey']
        if message.author == self.user:
            return

        message_Content = message.content

        if len(message_Content) > 0:
            for word in word_list:
                if word in message_Content:

                    query = message_Content
                    stopwords = ['/encryptkey']
                    querywords = query.split()

                    resultwords  = [word for word in querywords if word.lower() not in stopwords]
                    result11 = ' '.join(resultwords)

                    result = encrypt(result11.upper())
                    await message.delete()
                    await message.channel.send(result)

                    last_crypt = decrypt(result.upper())
                    print(result)
                    print(last_crypt)

                    async def on_message(self, message):
                        if message.author == self.user:
                            return
                        
                        message_Content = message.content

                        if len(message_Content) > 0:
                            if message_Content == "/decryptkey":
                                await client.send_message(message.author, last_crypt)

client = MyClient()
client.run('#token_here')

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您可以使用discord.Member.send 向用户发送 DM。在这种情况下,您需要message.author.send,而不是message.channel.send

    【讨论】:

    • 感谢您的反馈,但它不起作用,它说“启用 tracemalloc 以获取对象分配回溯”
    • 前面需要await
    • 谢谢它的工作,但因为你没有回答整个问题,我不会将其标记为已解决谢谢。
    • 在有人写了“/encryptkey 消息”之后,如果有人写了“/decryptkey”,它会向他们发送一个带有解密文本的私人 dm
    • 你的意思是子命令吗?为此,您将需要命令扩展,这样更容易
    猜你喜欢
    • 2022-06-11
    • 2021-12-25
    • 2019-06-22
    • 2020-06-16
    • 2021-02-24
    • 2021-03-11
    • 1970-01-01
    • 2019-09-19
    • 2020-12-02
    相关资源
    最近更新 更多