如何使我的Discord Bot发送消息,好像在Python中使用打印功能一样?
我正在使用Python编码我的第一个Discord Bot,但我仍然适应异步的工作原理。据我所知,它需要一个触发事件,例如接收消息,才能被激活。但是,我希望能够像使用print()方法一样将消息打印到Discord聊天中,并根据需要
进行 输入请求,例如使用Input()但是
class Game(commands.Cog, name="Ruby Palace"):
def __init__(self, bot):
self.bot = bot
self.playing = False
self.player_character = None
self.battle = None
@commands.command()
async def play(self, ctx):
await ctx.channel.send('Would you like to play a game?')
msg = await self.bot.wait_for("message")
if cm.y_n(msg):
await ctx.channel.send("Alright! Let's get started!")
self.playing = True
#i wanna change this next part so I can just call methods that will handle this job and make the process cleaner and compartmentalized
await ctx.channel.send("What class would you like to play? Fighter/Rogue/Mage")
player_class = await self.bot.wait_for("message")
await ctx.channel.send("What is your character's name?")
name = await self.bot.wait_for("message")
self.create_character(player_class.content, name.content)
await ctx.channel.send(f"Congratulations! {self.player_character} has been created!")
self.start_battle()
else:
await ctx.channel.send('uh-oh')
def create_character(self, p_class, name):
self.player_character = p_class(name)
def setup(bot):
bot.add_cog(Game(bot))
,我希望能够做些接近的事情
import os
from abilities import *
from random import randint
from discord.ext import commands
import global_cmds as cm
import units
enemy1 = Unit("Ted")
enemy2 = Unit("")
class Game(commands.Cog, name="Ruby Palace"):
def __init__(self, bot):
self.bot = bot
self.playing = False
self.player_character = None
self.battle = None
@commands.command()
async def play(self, ctx):
await ctx.channel.send('Would you like to play a game?')
msg = await self.bot.wait_for("message")
if cm.y_n(msg):
await ctx.channel.send("Alright! Let's get started!")
self.playing = True
# if i can do it this way, i can add new code and adjust existing code more easily, but I can't figure out how to do it
self.create_character()
else:
await ctx.channel.send('uh-oh')
def create_character(self):
send "What class"
class = response
send "what's the name"
name = response
self.player_character = class(name)
def setup(bot):
bot.add_cog(Game(bot))
I'm using python to code my first Discord bot, and I'm still getting used to how the async works. From what I can tell, it needs a triggering event, such as receiving a message, in order to be activated. However, I want to be able to just print messages to the discord chat as if using the print() method, and request for input as needed like using input()
I've managed to get an initial Cog for my game set up like this
class Game(commands.Cog, name="Ruby Palace"):
def __init__(self, bot):
self.bot = bot
self.playing = False
self.player_character = None
self.battle = None
@commands.command()
async def play(self, ctx):
await ctx.channel.send('Would you like to play a game?')
msg = await self.bot.wait_for("message")
if cm.y_n(msg):
await ctx.channel.send("Alright! Let's get started!")
self.playing = True
#i wanna change this next part so I can just call methods that will handle this job and make the process cleaner and compartmentalized
await ctx.channel.send("What class would you like to play? Fighter/Rogue/Mage")
player_class = await self.bot.wait_for("message")
await ctx.channel.send("What is your character's name?")
name = await self.bot.wait_for("message")
self.create_character(player_class.content, name.content)
await ctx.channel.send(f"Congratulations! {self.player_character} has been created!")
self.start_battle()
else:
await ctx.channel.send('uh-oh')
def create_character(self, p_class, name):
self.player_character = p_class(name)
def setup(bot):
bot.add_cog(Game(bot))
However, I want to be able to do something closer to this
import os
from abilities import *
from random import randint
from discord.ext import commands
import global_cmds as cm
import units
enemy1 = Unit("Ted")
enemy2 = Unit("")
class Game(commands.Cog, name="Ruby Palace"):
def __init__(self, bot):
self.bot = bot
self.playing = False
self.player_character = None
self.battle = None
@commands.command()
async def play(self, ctx):
await ctx.channel.send('Would you like to play a game?')
msg = await self.bot.wait_for("message")
if cm.y_n(msg):
await ctx.channel.send("Alright! Let's get started!")
self.playing = True
# if i can do it this way, i can add new code and adjust existing code more easily, but I can't figure out how to do it
self.create_character()
else:
await ctx.channel.send('uh-oh')
def create_character(self):
send "What class"
class = response
send "what's the name"
name = response
self.player_character = class(name)
def setup(bot):
bot.add_cog(Game(bot))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论