繁体   English   中英

如何获取 cmd 输入并使用 discord.py 发送

[英]How to get a cmd input and sending it with discord.py

所以我做了一个程序,其中 Discord 客户端在 CMD 中运行,现在遇到了障碍。 所以我想从 CMD 发送消息,但这样做会使输入不起作用或机器人不起作用。

主要的:

import discord
import messages
import sndmessage as snd
warningz = '\nI, the user will not use this program for any harmful attempts.'
bot = discord.Client()

def check(qw):
    if qw.lower() == 'n':
        quit()

print(warningz)
yousure = input('\nDo you agree to this simple sentence? Y/N: ')
check(yousure)

token = input('Please enter token: ')
name = input('Input username: ')
channel = input('Set default channel: ')
# start services
print('Starting...')
messages.channelid = channel
def getinput():
    while True:
        a = input()
        snd.sendmsg(a)

messages.run(token)

消息接收者(消息模块):

import discord
from discord.ext import tasks
import sndmessage
import logging
import datetime

bot = discord.Client()
logging.basicConfig(level=logging.INFO)
channelid = 0

async def activate():
    sndmessage.channelid = channelid

@bot.event
async def on_message(msg):
    ww = datetime.datetime.now()
    if msg.author != bot.user:
        auth = msg.author
        msg = msg.content
        print(str(auth)+' ['+str(ww)+']',msg)
def run(a):
    """Runs the bot with the token given by the user."""
    bot.run(a)

消息发送者(我认为这将是有用的 idk) sndmessage 模块

import discord
import logging
import datetime

bot = discord.Client()
logging.basicConfig(level=logging.INFO)
channelid = 0

@bot.event
async def sendmsg(a):
    channel = bot.get_channel(channelid)
    await channel.send(a)

if __name__ = '__main__' #not done here yet i think its useful but its just an opinion
def run(a):
    """Runs the bot with the token given by the user."""
    bot.run(a)

我也确实需要改进这一点,所以你们也可以就此提供提示和评论。 谢谢!

编辑

新的和改进的代码对我来说有一个更令人困惑的错误。

主要的:

import discord
import datetime
import logging
import time
import send
import threading
import io
from termcolor import colored,cprint
warningz = '\nI, the user will not use this program for any harmful attempts.'
bot = discord.Client()
logging.basicConfig(level=logging.INFO)
inputthread = threading.Thread(target=send.startloop)
channelid = 0
active = 0 


# services
class properties:
    active = 0 # not useful, will get deleted sometime in the future

@bot.event
async def reciever():
    latest = io.open('msgcontainer.txt','+r')
    a = latest.read()
    channel = bot.get_channel(int(channelid))
    channel.send(a)
    latest.flush()
    print('success')
recieverthread = threading.Thread(target=reciever)

@bot.event
async def on_message(msg):
    ww = datetime.datetime.now()
    if msg.author != bot.user:
        auth = msg.author
        msg = msg.content
        print(colored(str(auth),'cyan')+' ['+colored(str(ww),'yellow')+'] ',msg)

def check(qw):
    if qw.lower() == 'n':
        quit()
@bot.event
async def on_ready():
    cprint('Login successful!','green')
    print('Please give the functions 10 seconds to start')
    time.sleep(10)
    recieverthread.start()
    inputthread.start()
print(warningz)
yousure = input('\nDo you agree to this simple sentence? Y/N: ')
check(yousure)

#token = input('Please enter token: ')
#name = input('Input username: ')
#channel = input('Set default channel: ')
channelid = 0000000000000 #please replace with channel in final product
# start bot
try:
    bot.run('theamonglicode') #replace with token
except:
    print('You passed an improper token.')

原型制作时出错:

Exception in thread Thread-1 (startloop):
Traceback (most recent call last):
  File "D:\Python-Program\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "D:\Python-Program\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\user\Downloads\Terminality\send.py", line 7, in startloop
    msg = input()
  File "C:\Users\user\Downloads\Terminality\main.py", line 29, in msginput
    defchannel.send(latest.read())
AttributeError: 'NoneType' object has no attribute 'send'

以上是通过defchannel = bot.get_channel(channelid)发送消息的尝试。

下一次尝试没有发送任何内容,也没有出错。

第三次尝试是改进的主代码,报错:

D:\Python-Program\lib\threading.py:953: RuntimeWarning: coroutine 'reciever' was never awaited
  self._target(*self._args, **self._kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

编辑 2

导致问题的代码

import discord
import io
import sys
import threading
import send
channelid = 000000000000000000
bot = discord.Client()
def reciever(event,args):
    if event == 'builtins.input':
        latest = io.open('msgcontainer.txt','+r')
        a = latest.read()
        channel = bot.get_channel(int(channelid))
        channel.send(a)
        latest.flush()
        print('success')
loop =threading.Thread(target=startloop)
loop.start()
sys.addaudithook(reciever)
bot.run('apple')

我让它能够自己运行

如果您只想向您的 discord 发送 TOS 确认,您应该只使用 webhook。 在最终用户产品中实施机器人并不是一个好主意,因为人们得到令牌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM