Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';t';如果在';使用discord.py的语句?_Python_If Statement_Discord_Discord.py - Fatal编程技术网

Python 为什么不是';t';如果在';使用discord.py的语句?

Python 为什么不是';t';如果在';使用discord.py的语句?,python,if-statement,discord,discord.py,Python,If Statement,Discord,Discord.py,我正在处理的Discord bot有以下代码: import discord import random from discord.ext import commands, tasks import os import time import asyncio import re import urllib.request import json from apiclient.discovery import build from itertools import product, cycle

我正在处理的Discord bot有以下代码:

import discord
import random 
from discord.ext import commands, tasks
import os
import time
import asyncio
import re
import urllib.request
import json
from apiclient.discovery import build
from itertools import product, cycle
from discord.ext.tasks import loop

client = commands.Bot(command_prefix =  'v!', description='fff', case_insensitive=True)

token = 'REDEACTED'
client.remove_command("help")

@client.command(pass_context=True)
async def Ban(ctx):

    members = []

    a = (ctx.author)
    print(a)

    m = (ctx.message.content)

    m = m.replace("v!ban ", '')
    print(m)

    with open('members.txt', 'w'): pass

#    print(members)
    for member in ctx.guild.members:

        with open('members.txt', 'a', encoding = "UTF-8") as f:
            f.writelines(str(member) + '\n')

    with open('members.txt', 'r', encoding = "UTF-8") as f:
        members = f.read()

    for i in members:
        if i == m:
            print('True')
        else:
            print("False")

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await client.change_presence(activity = discord.Game("v!help"))

client.run(token)
“members.txt”文件包含:(我的Discord服务器的成员)

如果我执行命令
v!如果禁用UltimateDucc#9121
,它将返回
False
,而不是
True
,即使数组中存在此值

我正在努力实现的目标:

收集服务器成员-完成

放入文件-完成

从用户处获取输入-完成

检查输入是否在文件中-卡滞

非常感谢您的帮助。

f.read()
将返回包含文件内容的字符串。
当您循环遍历它时,
i
将是该字符串中的每个字符

您应该使用
list(f)
f.readlines()
并在结尾处去掉换行符


有关详细信息,请参阅。

添加了注释以进行说明,但基本问题是,您对文件中已加载列表中的所有字符进行了迭代,并检查它们是否都存在 等于用户提供的字符串

@client.command(pass\u context=True)
异步def禁令(ctx):
成员=[]
a=(ctx.author)
印刷品(a)
m=(ctx.message.content)
m=m.替换(“v!ban”,“”)
打印(m)
打开('members.txt','w'):通过
#我在这里交换了顺序,因为否则每次迭代都会打开文件
将open('members.txt','a',encoding=“UTF-8”)作为f:
对于ctx.guild.members中的成员:
f、 write(str(member)+'\n')#这里不必使用writeline,因为您只写一行
将open('members.txt',r',encoding=“UTF-8”)作为f:
members=f.read().split('\n')#我们需要一个成员列表,而不是包含所有成员的字符串
#我们可以在这里使用“in”操作符,它检查字符串是否在已加载列表中
如果成员中有m名成员:
打印('True')
其他:
打印(“假”)
不要使用此选项:

# code block 1
for i in members:
    if i == m:
        print('True')
    else:
        print("False")
使用以下命令:

# code block 2
if i in members:
    print('true')
else:
    print('false')
# code block 3 
x = members.split('\n')
if i in members:
    print('true')
else:
    print('false')

使用以下命令:

# code block 2
if i in members:
    print('true')
else:
    print('false')
# code block 3 
x = members.split('\n')
if i in members:
    print('true')
else:
    print('false')
在代码块1中: 比较是逐字符进行的。 您正在将文件中的每个字符与用户输入的字符串进行比较

在代码块2中: Python在给定字符串中查找子字符串。 ie:如果文件内容中存在用户输入的字符串,则返回True

在代码块3中:
将文件内容逐行拆分,并将每个条目存储在数组中。然后查看用户输入的字符串是否在此数组中。

如果。。。在任何位置。不过我很好奇,open('members.txt','w'):pass的
是什么意思?这是指
for I in members:
这一行清除了文本文件。如果我在m:
中,你的意思是
吗?是的,'m'是我没有投反对票的用户名,但我猜投反对票是因为你没有解释就发布了代码。您的编辑在一定程度上解决了这个问题。@byxor我明白了,但他们可以留下评论让我知道。