瀏覽代碼

First commit

pull/2/head
Jérôme Deuchnord 3 年之前
當前提交
f7bae9ec8b
共有 4 個文件被更改,包括 246 次插入0 次删除
  1. +1
    -0
      .gitignore
  2. +13
    -0
      Pipfile
  3. +122
    -0
      Pipfile.lock
  4. +110
    -0
      bot.py

+ 1
- 0
.gitignore 查看文件

@@ -0,0 +1 @@
config.json

+ 13
- 0
Pipfile 查看文件

@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
irc = "*"
irc3 = "*"

[dev-packages]

[requires]
python_version = "3.9"

+ 122
- 0
Pipfile.lock 查看文件

@@ -0,0 +1,122 @@
{
"_meta": {
"hash": {
"sha256": "f8a2cfa6ec2a8e170d01bfb890ffdff40688042bda4fddbd8d7f6e74dd205b21"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.9"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"docopt": {
"hashes": [
"sha256:49b3a825280bd66b3aa83585ef59c4a8c82f2c8a522dbe754a8bc8d08c85c491"
],
"version": "==0.6.2"
},
"irc": {
"hashes": [
"sha256:99fd5d1fa1d054dee4fbb81e0d5193dc1e8200db751d5da9a97850a62162b9ab",
"sha256:b5179428448947d364edcdcef8cb34a2a7033b4e78e58fec32d092091eab786f"
],
"index": "pypi",
"version": "==19.0.1"
},
"irc3": {
"hashes": [
"sha256:088b7be88817dee5d02e7e06c9dbf503c8e807a8cccc9b36b5be419918574cf1",
"sha256:6800c8876a889736961e199e834dc9933244affa20d4da086fb4b47f76984e12"
],
"index": "pypi",
"version": "==1.1.7"
},
"jaraco.classes": {
"hashes": [
"sha256:22ac35313cf4b145bf7b217cc51be2d98a3d2db1c8558a30ca259d9f0b9c0b7d",
"sha256:ed54b728af1937dc16b7236fbaf34ba561ba1ace572b03fffa5486ed363ecf34"
],
"markers": "python_version >= '3.6'",
"version": "==3.2.1"
},
"jaraco.collections": {
"hashes": [
"sha256:3662267424b55f10bf15b6f5dee6a6e48a2865c0ec50cc7a16040c81c55a98dc",
"sha256:fa45052d859a7c28aeef846abb5857b525a1b9ec17bd4118b78e43a222c5a2f1"
],
"markers": "python_version >= '3.6'",
"version": "==3.3.0"
},
"jaraco.functools": {
"hashes": [
"sha256:7c788376d69cf41da675b186c85366fe9ac23c92a70697c455ef9135c25edf31",
"sha256:bfcf7da71e2a0e980189b0744b59dba6c1dcf66dcd7a30f8a4413e478046b314"
],
"markers": "python_version >= '3.6'",
"version": "==3.3.0"
},
"jaraco.logging": {
"hashes": [
"sha256:150dc8701207b28bc65a16f0e91c07250a8d1b9da324ce674c0e375774944f13",
"sha256:ec4720a1ed8d7e4667a5f0178c4864df1a2f085aad5ba47355c41fcb2f9d68cd"
],
"markers": "python_version >= '3.6'",
"version": "==3.1.0"
},
"jaraco.stream": {
"hashes": [
"sha256:0ede0988de595d15a72fa95f1eec4dee20fdede30e8d2ddee1b9c2a963eb67ef",
"sha256:86c57fedffd4d5a4b18817f99ddf62ac8ed0a1bc31a1c41b9a88df9c6bb56e0b"
],
"markers": "python_version >= '3.6'",
"version": "==3.0.2"
},
"jaraco.text": {
"hashes": [
"sha256:b647f2bf912e201bfefd01d691bf5d603a94f2b3f998129e4fea595873a25613",
"sha256:f07f1076814a17a98eb915948b9a0dc71b1891c833588066ec1feb04ea4389b1"
],
"markers": "python_version >= '3.6'",
"version": "==3.5.0"
},
"more-itertools": {
"hashes": [
"sha256:2cf89ec599962f2ddc4d568a05defc40e0a587fbc10d5989713638864c36be4d",
"sha256:83f0308e05477c68f56ea3a888172c78ed5d5b3c282addb67508e7ba6c8f813a"
],
"markers": "python_version >= '3.5'",
"version": "==8.8.0"
},
"pytz": {
"hashes": [
"sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da",
"sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798"
],
"version": "==2021.1"
},
"tempora": {
"hashes": [
"sha256:10fdc29bf85fa0df39a230a225bb6d093982fc0825b648a414bbc06bddd79909",
"sha256:d44aec6278b27d34a47471ead01b710351076eb5d61181551158f1613baf6bc8"
],
"markers": "python_version >= '3.6'",
"version": "==4.0.2"
},
"venusian": {
"hashes": [
"sha256:06e7385786ad3a15c70740b2af8d30dfb063a946a851dcb4159f9e2a2302578f",
"sha256:f6842b7242b1039c0c28f6feef29016e7e7dd3caaeb476a193acf737db31ee38"
],
"markers": "python_version >= '3.5'",
"version": "==3.0.0"
}
},
"develop": {}
}

+ 110
- 0
bot.py 查看文件

@@ -0,0 +1,110 @@
#!/usr/bin/env python3

import irc3
import json
from os import environ


TWITCH_IRC_SERVER = "irc.chat.twitch.tv"
TWITCH_IRC_PORT = 6697


class Command:
name: str
message: str

def __init__(self, name: str, message: str):
self.name = name
self.message = message

@classmethod
def from_dict(cls, params: dict):
return Command(params['name'], params['message'])


class Config:
channel: str
nickname: str
token: str
commands: [Command]

def __init__(self, channel: str, nickname: str, token: str, commands: [Command] = None):
self.nickname = nickname
self.channel = channel
self.token = token
self.commands = commands if commands is not None else []

@classmethod
def from_dict(cls, params: dict, token: str):
commands = []

for command in params['commands']:
commands.append(Command.from_dict(command))

return Config(
params['channel'],
params['nickname'],
token,
commands
)


@irc3.plugin
class TwitchBot:
def __init__(self, bot):
self.config = get_config()
self.bot = bot
self.log = self.bot.log

def connection_made(self):
print('connected')

def server_ready(self):
print('ready')

def connection_lost(self):
print('connection lost')

@irc3.event(irc3.rfc.PRIVMSG)
def on_msg(self, target, mask, data, event):
author_name = mask.split('!')[0]

for command in self.config.commands:
print(command.name)
print('%s ' % data)
if ('%s ' % data).startswith('!%s ' % command.name):
self.bot.privmsg(target, command.message)

@irc3.event(irc3.rfc.JOIN)
def on_join(self, mask, channel, **kw):
print('JOINED')
print(mask)
print(channel)
print(kw)


def get_config():
with open('config.json', 'r') as config_file:
token = environ['TWITCH_TOKEN']
return Config.from_dict(json.loads(config_file.read()), token)


def main() -> int:
config = get_config()
bot = irc3.IrcBot.from_config({
'nick': config.nickname,
'password': config.token,
'autojoins': ['%s' % config.channel],
'host': TWITCH_IRC_SERVER,
'port': TWITCH_IRC_PORT,
'ssl': True,
'includes': [__name__]
})

bot.run(forever=True)

return 0


if __name__ == '__main__':
exit(main())

Loading…
取消
儲存