指示机器人是否在聊天室或 PM 中响应
Dictating whether or not bot responds in chatroom, or in PMs
我正在尝试弄清楚如何让我的机器人区分通过聊天室发送响应还是通过 PM 发送响应。目前,它似乎正在为 name
方法做这两件事。我可以在聊天室或直接向机器人发送消息时使用 !name 命令,它会始终这样回复(如果在聊天中发送,将在聊天中回复。如果在 PM 中发送,将在 PM 中回复)
对于stats
命令,它至少只在私信中响应,不管是在聊天中使用!stat命令,还是在私信中。
我在文档中读到,如果你将装饰器设置为 no_pm=True
那么该命令在 PM 中被禁止为 运行,但它仍然在 [=12= 的情况下运行]方法。
BotCommands.py
import discord
import os
from discord.ext import commands
token = open("token.txt", "r").read()
client = commands.Bot(command_prefix = '!')
@client.command()
async def load(ctx, extension):
client.load_extension("cogs." + extension)
@client.command()
async def unload(ctx, extension):
client.unload_extension("cogs." + extension)
for filename in os.listdir("./cogs"):
if filename.endswith('.py'):
client.load_extension("cogs." + filename[:-3])
client.run(token)
charCreation.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(on_pm=True)
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 = ctx.author.send
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("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("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")
with open(charFolder + player + ".txt", "r+", encoding="utf-8") as file:
print("Am I here?")
charData = json.load(file)
charData["strength"] = int(strength)
charData["dexterity"] = int(dexterity)
charData["constitution"] = int(constitution)
charData["hit"] = int(charData["hit"] + strMod)
charData["damage"] = int(charData["damage"] + strMod)
charData["ac"] = int(charData["ac"] + dexMod)
charData["hp"] = int(charData["hp"] + conMod)
file.seek(0)
file.write(json.dumps(charData, ensure_ascii=False, indent=2))
file.truncate()
file.close()
我希望 name
方法只能在聊天室中使用,绝不能在 PM 中使用;和 stats
命令只能在 PM 中使用,永远不能在聊天室中使用。事实上,如果可能的话,我希望机器人在有人试图在聊天室中使用它时做出类似 'The !stats command MUST be used in a PM with me' 的响应。
您正在寻找 guild_only
and dm_only
checks。用法类似于:
@commands.command()
@commands.guild_only()
async def name(self, ctx, name):
...
@name.error
async def name_error(ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.send("The command !name may not be used in a private channel!")
else:
raise error
我正在尝试弄清楚如何让我的机器人区分通过聊天室发送响应还是通过 PM 发送响应。目前,它似乎正在为 name
方法做这两件事。我可以在聊天室或直接向机器人发送消息时使用 !name 命令,它会始终这样回复(如果在聊天中发送,将在聊天中回复。如果在 PM 中发送,将在 PM 中回复)
对于stats
命令,它至少只在私信中响应,不管是在聊天中使用!stat命令,还是在私信中。
我在文档中读到,如果你将装饰器设置为 no_pm=True
那么该命令在 PM 中被禁止为 运行,但它仍然在 [=12= 的情况下运行]方法。
BotCommands.py
import discord
import os
from discord.ext import commands
token = open("token.txt", "r").read()
client = commands.Bot(command_prefix = '!')
@client.command()
async def load(ctx, extension):
client.load_extension("cogs." + extension)
@client.command()
async def unload(ctx, extension):
client.unload_extension("cogs." + extension)
for filename in os.listdir("./cogs"):
if filename.endswith('.py'):
client.load_extension("cogs." + filename[:-3])
client.run(token)
charCreation.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(on_pm=True)
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 = ctx.author.send
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("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("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")
with open(charFolder + player + ".txt", "r+", encoding="utf-8") as file:
print("Am I here?")
charData = json.load(file)
charData["strength"] = int(strength)
charData["dexterity"] = int(dexterity)
charData["constitution"] = int(constitution)
charData["hit"] = int(charData["hit"] + strMod)
charData["damage"] = int(charData["damage"] + strMod)
charData["ac"] = int(charData["ac"] + dexMod)
charData["hp"] = int(charData["hp"] + conMod)
file.seek(0)
file.write(json.dumps(charData, ensure_ascii=False, indent=2))
file.truncate()
file.close()
我希望 name
方法只能在聊天室中使用,绝不能在 PM 中使用;和 stats
命令只能在 PM 中使用,永远不能在聊天室中使用。事实上,如果可能的话,我希望机器人在有人试图在聊天室中使用它时做出类似 'The !stats command MUST be used in a PM with me' 的响应。
您正在寻找 guild_only
and dm_only
checks。用法类似于:
@commands.command()
@commands.guild_only()
async def name(self, ctx, name):
...
@name.error
async def name_error(ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.send("The command !name may not be used in a private channel!")
else:
raise error