我试图弄清楚如何通过命令在播放器和机器人之间启动直接消息。该命令应起作用,以便播放器DM的机器人具有!stats和三个数字(例如:!stats 6 8 1),并且该机器人将响应并分配适当的统计信息。 (例如:您在力量上放置了6点,在敏捷上放置了8点,在体质上放置了1点。)
我的以下代码是这样的:
sudo service plexmediaserver restart
BotCommands.py
import discord
from discord.ext import commands
import os
import json
from pathlib import Path
class Character(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Bot is Online")
@commands.command()
async def name(self, ctx, name):
player = str(ctx.message.author)
path = os.getcwd()
charFolder = os.path.join(path + "/characters/")
charFile = Path(charFolder + player + ".txt")
# Get the name of character being created
if charFile.is_file():
await ctx.send("You've already created a character, dumbass.")
else:
await ctx.send("I did it!")
await ctx.send("Your character name is: " + name)
await ctx.send("Your character sheet has been created.")
levelDict = {1: [25, 1, 6, 15, 2, 1, 5, 75]}
characterFile = {}
level = 1
xp = 0
characterFile["name"] = name
characterFile["level"] = level
hp = levelDict[1][0]
characterFile["hp"] = hp
tFeats = levelDict[1][4]
characterFile["total feats"] = tFeats
numberOfDice = levelDict[1][1]
numberOfSides = levelDict[1][2]
characterFile["base damage"] = str(numberOfDice) + "d" + str(numberOfSides)
characterFile["hit"] = levelDict[1][5]
characterFile["damage"] = levelDict[1][5]
characterFile["ac"] = levelDict[1][6]
characterFile["currentxp"] = xp
nextLevel = levelDict[1][7]
characterFile["nextlevel"] = nextLevel
characterFile["strength"] = 0
characterFile["dexterity"] = 0
characterFile["constitution"] = 0
characterFile["remaining feats"] = 2
ap = levelDict[1][3]
characterFile["total ap"] = ap
hasTaken = []
characterFile["feats taken"] = hasTaken
file = open(charFolder + player + ".txt", "w", encoding="utf-8")
json.dump(characterFile, file, ensure_ascii=False, indent=2)
await ctx.send("PM me with '!stats <str> <dex> <con>' to set your abilities. Wouldn't want everyone "
"to see your secrets, would we?")
@commands.command()
async def stats(self, ctx, strength, dexterity, constitution):
private = self.client.send_message
player = str(ctx.message.author)
path = os.getcwd()
charFolder = os.path.join(path + "/characters/")
charFile = Path(charFolder + player + ".txt")
if not charFile.is_file():
await private(player, "You don't even have a character created yet. Type !name <name> in the room. "
"Where <name> is your character's actual name. (Example: !name Joe")
else:
strMod = int(int(strength) / 2)
dexMod = int(int(dexterity) / 2)
conMod = int(int(constitution) * 5)
print(strMod, dexMod, conMod)
await private(player, "Allocating the following: \n\n"
"Strength: " + strength + " (+" + str(strMod) + " bonus to hit and damage.)\n"
"Dexterity: " + dexterity + " (+" + str(dexMod) + " bonus to armor class.)\n"
"Constitution: " + constitution + " (+" + str(conMod) + " bonus to armor class.)\n")
这将返回错误:
discord.ext.commands.errors.CommandInvokeError:命令引发了异常:AttributeError:'Bot'对象没有属性'send_message'
我需要在BotCommands.py中启动一些“ on_message”命令吗?还是我只是说方法错误?
答案 0 :(得分:1)
# BotCommands.py
...
client = commands.Bot(command_prefix = '!')
...
# code.py
...
@commands.command()
async def stats(self, ctx, strength, dexterity, constitution):
private = self.client.send_message # <--
...
discord api已更改
来自https://discordpy.readthedocs.io/en/latest/migrating.html?#sending-messages
所做的更改之一是将以前的Client.send_message和Client.send_file功能合并到单个方法send()中。
基本上:
# before
await client.send_message(channel, 'Hello')
# after
await channel.send('Hello')