如何使我的Discord Bot发送消息,好像在Python中使用打印功能一样?

发布于 2025-02-12 22:14:48 字数 2659 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文