Python 在不和谐聊天中打印4x4表情网格的功能

Python 在不和谐聊天中打印4x4表情网格的功能,python,python-3.x,discord.py,Python,Python 3.x,Discord.py,这里是新的python爱好者 我目前正在制作一个discord机器人,当特定用户在聊天中键入以下内容时,该机器人会在4x4网格中以16张图片作出响应: XXXX XXXX XXXX XXXX 这是我当前的代码: import discord from secrets import TOKEN from emotes import * # user IDs bonvang_ID = "417010736988946444" djursing_ID = "12480604656802201

这里是新的python爱好者

我目前正在制作一个discord机器人,当特定用户在聊天中键入以下内容时,该机器人会在4x4网格中以16张图片作出响应:

XXXX

XXXX

XXXX

XXXX
这是我当前的代码:

import discord
from secrets import TOKEN
from emotes import *

# user IDs
bonvang_ID = "417010736988946444"
djursing_ID = "124806046568022016"
jonathan_ID = "151788707760832512"
alex_ID = "151745760935936001"
snuffles_ID = "221360712222507009"

# bot = commands.Bot(command_prefix='!')
client = discord.Client()

# prints message in terminal when bot is ready
@client.event
async def on_ready():
    print(f"Logged in as {client.user}")


# @client.event
# async def on_member_join(member):
#     await message.channel.send(f"""Welcome to the server {member}!\n
#         I'm just a simple bot made by Mr.Nofox.\n
#         If you want to know more about the commands, ask him or try your luck with the others! :)""")


def member_amount():
    # create a variable that contains all the servers
    active_servers = client.servers
    # create a variable to store amount of members per server
    sum = 0
    # loop through the servers, get all members and add them up, this includes bots
    for s in active_servers:
        sum += len(s.members)
    return sum


def picture_grid():
    return (f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n)


@client.event
async def on_message(message):
    # print(f"{message.channel}: {message.author}: \"{message.content}\"")
    guild = client.get_all_members()

    if "!count()" == message.content:
        await client.send_message(message.channel, f"```py\nTotal members: {member_amount()}```")

    elif "!report()" == message.content:
        online = 0
        idle = 0
        offline = 0

        for member in guild:
            if str(member.status) == "online":
                online += 1
            if str(member.status) == "offline":
                offline += 1
            if str(member.status) == "idle" or str(member.status) == "dnd":
                idle += 1

        await client.send_message(message.channel, f"```Online: {online}\nIdle/busy.dnd: {idle}\nOffline: {offline}```")


    #TODO: Make a neat function for the pictures in message that responds with 4x4 image in chat when a given user types
    if (message.author.id == alex_ID or message.author.id == snuffles_ID):
        await client.send_message(message.channel, "DET ER MIN SØN!")
        await client.send_message(message.channel, picture_grid())


# add bot-token found on website
client.run(TOKEN)
所有的emotes都在一个名为“emotes.py”的文件中,我导入了这个文件。 每个emote都是一个字符串代码,例如:
pic01=''

我用这条线在自动取款机上取钱。是:

await client.send_message(message.channel, f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n")
这是一种糟糕的编码实践,因此我想创建一个返回:

f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n"
我最初的想法是:

def picture_grid():  
    return (f"{pic01} {pic02} {pic03} {pic04}\n{pic05} {pic06} {pic07} {pic08}\n{pic09} {pic10} {pic11} {pic12}\n{pic13} {pic14} {pic15} {pic16}\n")
这将执行此操作,并将第一个代码转换为:

await client.send_message(message.channel, picture_grid())
但是仅仅是把坏代码移到另一个地方。。我现在被一项简单的任务困住了


有什么想法吗?

您发送的消息每次都有完全相同的文本。为什么不将其存储为文本文件,然后
f.read()
将该文件作为您的邮件正文呢?非常感谢@PatrickHaugh,非常有魅力。)您发送的消息每次都有完全相同的文本。为什么不将其存储为文本文件,然后
f.read()
将该文件作为您的邮件正文呢?非常感谢@PatrickHaugh,非常有魅力。)