当使用命令时PMing多个用户

2024-04-19 18:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个机器人中有一个命令,允许具有特定角色的人用货币奖励其他用户。我正在尝试设置它,以便在使用命令时,bot将向用户发送DM,通知他们奖励,然后向我(bot的所有者)发送DM,通知我该操作,以及为什么会给出奖励

我有一个文件设置,它有一个将用户名与用户ID关联的字典。因此,当您键入,例如:!rewardgold 100 Joe'时,它接受'joe'并在数据库中查找它,以查看是否有一个ID与之关联。在这方面,我已经有了Joe的唯一用户ID,显然我也有自己的用户ID。所以,这应该只是把DM发送给每个人的问题。为了完成,我目前在下面的代码中使用了ctx.send(),但是我希望用DM来代替它

另外@commands.has_role("Admin")被注释掉了,因为尽管有用户名,我还是在使用命令测试“Admin”角色时总是出错。这是一个脚本:BotCommands.py

import os
import json

import asyncio
from pathlib import Path
from collections import Counter

from discord.ext import commands

from gamelogic import onMSGAccept, onMSGRoll, onMSGUtil, onPRIUtil

class Combat(commands.Cog):

    def __init__(self, client):
        self.client = client

    #!rewardgold <gold> <player> <reason> (If no reason given, defaults to "Not Specified")
    @commands.command()
    @commands.dm_only()
    #@commands.has_role("Admin")
    async def rewardgold(self, ctx, gold, player, reason="Not Specified"):

        msg = ""
        charFolder = characters()

        with open(charFolder + "playerDatabase.txt", 'r', encoding="utf-8") as file2:
            playerDatabase = json.loads(file2.read())
            file2.close()
        try:
            gifted = ""
            for item in playerDatabase.items():
                if item[0].lower() == player.lower():
                    gifted = item[1]

            giftedFile = open(charFolder + str(gifted) + ".txt", "r", encoding="utf-8")
            giftedData = json.load(giftedFile)
            giftedFile.close()
            giftedData['gold'] += int(gold)
            file = open(charFolder + str(gifted) + ".txt", "w", encoding="utf-8")
            json.dump(giftedData, file, ensure_ascii=False, indent=2)
            file.close()
            await ctx.send(player + " has been awarded  " + str(gold) +
                           " gold. (Reason: " + reason)
        except FileNotFoundError:
            await ctx.send(player + " does not have a character.")

我试着做我的家庭作业,但我在谷歌搜索的每件事都围绕着Client.send_message的使用,它在discord.py rewrite中已经停止使用,取而代之的是just send()。我不确定如何使用.send()将消息发送给Joe的uniqueID,而不是发送回最初使用该命令的人

我还查看了一个举行on_message的活动,但我的谷歌fu只展示了在频道室中以非常简单的方式工作的例子。所说的例子通常是听房间里的声音,看到输入的信息,然后再把它吐回房间。这没用。我的机器人启动脚本botCreation.py如下所示:

import discord
import os
import json

from discord.ext import commands

with open('token.txt', 'r+') as file:
    stuff = json.load(file)
    file.close()
token = stuff["token"]

client = commands.Bot(command_prefix = '!')
client.remove_command('help')

@client.command()
async def load(ctx, extension):
    client.load_extension("cogs." + extension)

@client.command()
async def unload(ctx, extension):
    client.unload_extension("cogs." + extension)

@client.command()
async def reload(ctx, extension):
    client.unload_extension("cogs." + extension)
    client.load_extension("cogs." + extension)

for filename in os.listdir("./cogs"):
    if filename.endswith('.py'):
        client.load_extension("cogs." + filename[:-3])

client.run(token)

任何帮助都将不胜感激


Tags: 用户fromimportclientsendjsondefextension